Skip to content

Instantly share code, notes, and snippets.

@GrantRobertson
Last active February 6, 2017 03:15
Show Gist options
  • Save GrantRobertson/38c5cec5d3bbef0075253189a7ded4e6 to your computer and use it in GitHub Desktop.
Save GrantRobertson/38c5cec5d3bbef0075253189a7ded4e6 to your computer and use it in GitHub Desktop.
Java example to illustrate the various possible locations for (kinds of) classes that can be within the same .java file... and other stuff
/* ExecutableTest.java
This example program is designed to illustrate:
A) The various possible locations for (kinds of) classes that can be within the same .java file.
B) Which of those classes can contain main methods which adhere to all the rules to be a "true"
main() method.
C) Which of those main() methods can be run from the command line.
D) How to access all of those main methods from within the "main" main() method.
E) It also happens to illustrate some of what can be called from within a static method.
F) And... one of the advantages of using a varargs parameter instead of a String array parameter
in a main() method declartion.
Copyright 2017 - Grant S. Robertson (for now)
*/
class ExecutableTest {
// This class is not declared public (even though that is the convention) so I can illustrate
// that making it public is not required in order to execute its main() method from the command
// line. You should also study access modifiers separately, as that topic is outside the scope
// of this example.
public static void main(String[] args) {
// Keep in mind that this main() method is declared static. Therefore it can only call other
// static methods or methods in instantiated objects.
System.out.println("Hello from ExecutableTest.");
// It is OK to call this method by using the class name and do so within this static method
// because the StaticNestedClass class and its main() method are both also static.
StaticNestedClass.main();
// Getting set up to call the main method in the inner class.
ExecutableTest myExecutbleTestObject = new ExecutableTest();
ExecutableTest.InnerClass myInnerObject = myExecutbleTestObject.new InnerClass();
myInnerObject.main();
// Yup. This is the rigamarole you have to go through in order to call that method in
// that inner class. You have to instantiate the outer class, then instantiate the
// inner class "inside" of (OK, actually, as a member of) said outer class object,
// THEN you can call the method. Why? Because that is the way Java is designed.
// Cannot call regularMethod() directly from this main() method because this method is
// static, while regularMethod() is not. So it is called as a method of the
// myExecutbleTestObject object which I previously instantiated.
myExecutbleTestObject.regularMethod();
// OK to call the staticMethod() method from within this main() method because it is static.
// Also OK to call this static method without using the classname
// (e.g. ExecutableTest.staticMethod();) because staticMethod() is a member of this class.
staticMethod();
// OK to call the main() method in the Other class via the class name rather than an
// instantited object reference because that main() method is static.
Other.main();
}
void regularMethod() {
// Local classes, unlike other kinds of class, must be declared before they are used.
// This is not even mentioned in the Java Tutorial.
class LocalToRegularMethod { // Local classes cannot be static.
public void main(String[] args) { // Methods in local classes cannot be static.
// Note use of String[] parameter.
System.out.println("Hello from LocalToRegularMethod.");
}
}
// Must instantiate the local class object (within the local scope) before it can be used.
LocalToRegularMethod myLocalToRegularObject = new LocalToRegularMethod();
myLocalToRegularObject.main(new String[] {} );
// Notice that I had to instantiate a new, empty, anonymous string array to pass to the
// main() method.
}
static void staticMethod() {
class LocalToStaticMethod { // Can't make local class static, even inside a static method.
public void main(String... args) { // Methods in local classes cannot be static.
// Note use of VarArgs parameter.
System.out.println("Hello from LocalToStaticMethod.");
}
}
LocalToStaticMethod myLocalToStaticObject = new LocalToStaticMethod();
myLocalToStaticObject.main();
// Notice that I didn't need to pass any parameters to this main() method. That is
// because a varargs parameter will take zero or more arguments.
}
private static class StaticNestedClass {
// Does not need to be private. This only illustrates that even IF the class is private
// you can still execute it from the command line as long as it has a "real" main() method.
public static void main(String... args) {
System.out.println("Hello from NestedClass.");
}
}
class InnerClass {
// While this class does produce a separate .class file when ExecutableTest.java is
// compiled, you cannot execute the class file because you cannot make the main() method
// static. See below.
// public static void main(String... args) {
// System.out.println("Hello from InnerClass.");
// }
// Does not compile because you can't have a static method inside a (non-static)
// inner class. This is because Java assumes all inner classes will ONLY exist as
// instances of objects within instances of their outer class. Meaning, the inner class
// will never be loaded and available unless it is instantiated. Therefore, there is no
// point in having static methods. Therefore, there is no point in allowing them.
public void main(String... args) { // Note lack of static modifier.
System.out.println("Hello from InnerClass.");
}
}
}
class Other {
public static void main(String... args) {
System.out.println("Hello from Other class.");
}
}
// After compiling this .java file, open a command prompt (terminal window) to the folder where the
// .class files are located (depends on how you compiled the file). Do a listing of the .class
// files in the folder then try to execute each class in turn using the java command.
// Hint: Three of the classes will run. See if you can figure out the common denomenator.
// Now, after showing what is technically possible, I should point out that the convention is to
// always use only one top-level class per file and make it public. This makes it easier to keep
// track of and find the file for each class. (ExecutableTest and Other are top-level classes.)
// In addition, one should study inner classes, static nested classes and local classes before
// trying to use them in real life. This example is only designed to show the possible ways to
// execute a main() method from the command line. Not the ins and outs of every other programmng
// concept used in this example.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment