Is there a way to get the class that was called at the command line? I have a superclass with several subclasses that inherit its main. I want to be able to determine which class was called at the command line so I can create an instance of that class. Here is an example of what I am trying to do:
You can't inherit main -- it's static. static methods do not participate in inheritance. Therefore, you know that the class that is running main was the class that was entered at the command line -- unless, of course, you called main from another class (which is permissible...main is just another method, after all).
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Joined: Mar 13, 2004
I don't want to be rude, but your answer is completely wrong. Static method are inherited and main can be inherited as well. Want proof? try this simple example. Create a file called mainTest.java with this code and compile it and run it as "java mainTest":
[ December 06, 2004: Message edited by: James Davies ]
Java can be invoked in a wide variety of ways, not just via the command-line "java" program. The idea that the static method "main" of some specified class is the entry point of the application is part of the "java" program's interface, not something fundamental to Java.
Perhaps that's why there is no concept of the applications's main class in the Java API. Whatever the reason, I don't think there's a built-in way to find out what class was run by the "java" command.
How much flexibility do you have? Could you always run the same class (your superclass), but provide the name of the subclass to be instantiated via one of the command-line arguments or via a system property? Could you write your own invoker program, using JNI invocation interface? That would free you completely from the constraints of "java" program, but is not trivial, so you'd need to be sure it was necessary.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
You can get this work if you define a static variable that holds the reference to the Class object of the Class that is invoked (Better understood by reading the source provided below. I have not considered cases on the real usability of the code. One can fine tune them as necessary.)
Joined: Oct 30, 2001
Originally posted by S. Sundar Raman: You can get this work if you define a static variable that holds the reference to the Class object of the Class that is invoked (Better understood by reading the source provided below. I have not considered cases on the real usability of the code. One can fine tune them as necessary.)
Well, that works and is quite clever. But I was assuming you can't add anything to the subclasses (you have added a static initialiser block). If you can add things to the subclass, why not do it the obvious way and put a main() in the subclass? The subclass main() would then call a method (called main() or something else) in the superclass, passing in whatever information is needed.