[ December 06, 2004: Message edited by: James Davies ]
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.
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.