Prev: in the Observer (Publish/Subscribe) pattern, are notificationsalien method calls?
Next: Using 'final' with local variables
From: Mike Schilling on 14 Sep 2009 20:08 Arne Vajh�j wrote: > Andreas Leitgeb wrote: >> Roedy Green <see_website(a)mindprod.com.invalid> wrote: >>>> I would like to write a base class from which child classes >>>> inherit a public static void main( String args[] ) method. >>> You naturally inherit all static methods, including main. >>> You can specify which generation you want by prefixing the class >>> name. e.g. Grandma.main Mom.main Me.main >>> If there is no Me.main, you can use Me.main to get an Mom.main. >> >> The real question was (as far as I understood it): >> >> Can the implementation of Mom.main() find out, whether the user >> used "Mom" or "Me" as startup class? Or perhaps even "Brother" >> or "Sister", which for the sake of this example also do not have >> their own main() but fall back to Mummy's? >> >> I don't know any way, and I doubt there even is one, but can't say >> for sure. Perhaps Java can be asked for the startup-class directly? > > If we can assume that Java version >= 1.5, main thread has > thread id 1 and max. stack depth is 1000: > > StackTraceElement[] ste = > ManagementFactory.getThreadMXBean().getThreadInfo(1, > 1000).getStackTrace(); String clznam = > ste[ste.length-1].getClassName(); Is that really going to give you the class mentioned in the command line rather than the class which defines the static main() method?
From: Arne Vajhøj on 14 Sep 2009 20:27 Mike Schilling wrote: > Arne Vajh�j wrote: >> Andreas Leitgeb wrote: >>> Roedy Green <see_website(a)mindprod.com.invalid> wrote: >>>>> I would like to write a base class from which child classes >>>>> inherit a public static void main( String args[] ) method. >>>> You naturally inherit all static methods, including main. >>>> You can specify which generation you want by prefixing the class >>>> name. e.g. Grandma.main Mom.main Me.main >>>> If there is no Me.main, you can use Me.main to get an Mom.main. >>> The real question was (as far as I understood it): >>> >>> Can the implementation of Mom.main() find out, whether the user >>> used "Mom" or "Me" as startup class? Or perhaps even "Brother" >>> or "Sister", which for the sake of this example also do not have >>> their own main() but fall back to Mummy's? >>> >>> I don't know any way, and I doubt there even is one, but can't say >>> for sure. Perhaps Java can be asked for the startup-class directly? >> If we can assume that Java version >= 1.5, main thread has >> thread id 1 and max. stack depth is 1000: >> >> StackTraceElement[] ste = >> ManagementFactory.getThreadMXBean().getThreadInfo(1, >> 1000).getStackTrace(); String clznam = >> ste[ste.length-1].getClassName(); > > Is that really going to give you the class mentioned in the command line > rather than the class which defines the static main() method? You are correct. It returns the parent class where main is defined. Arne
From: Mike Schilling on 14 Sep 2009 21:32 Arne Vajh�j wrote: >>> If we can assume that Java version >= 1.5, main thread has >>> thread id 1 and max. stack depth is 1000: >>> >>> StackTraceElement[] ste = >>> ManagementFactory.getThreadMXBean().getThreadInfo(1, >>> 1000).getStackTrace(); String clznam = >>> ste[ste.length-1].getClassName(); >> >> Is that really going to give you the class mentioned in the command >> line rather than the class which defines the static main() method? > > You are correct. It returns the parent class where main is > defined. Right. My strong impression is that trying to figure out which subclass was mentioned in the command line is simply hopeless (or at best extremely non-portable) and that, accordingly, the OP should figure out some other way of doing things. Fortunately, there are dozens of them.
From: Mike Amling on 14 Sep 2009 23:04 Ross wrote: > I could probably solve the problem if I could find a list of the > classes that are currently loaded. As only one of them will be an > instance of "Game", apart from "Game" (my parent class) itself. But > looking through the classloader interface, I can't seem to find that > out either. Your students are already compiling their code with some boilerplate, namely class Mine extends Game { .... } Could you change that boilerplate to invoke a constructor in Game via an implicit super(), and have the Game constructors leave what you need in one of Game's static variables where Game.main can find it? class Mine extends Game { static {new Mine();} .... } --Mike Amling
From: Patricia Shanahan on 14 Sep 2009 23:58
Ross wrote: .... > Unfortunately, I need to do this. The parent class is going to be > subclassed by young children, first experimental subject being a nine > year old. I have to kick out the main class, but still have it > runnable. Fundamentally, you can't teach advanced programming > languages with sophisticated structures to young children, or at least > most young children. I'm sure there are some prodigies out there. > Previously when I've done this before, I've created bespoke > programming languages with bespoke IDEs. But the whole point of what > I'm trying now is to see whether Object Orientated Programming can be > leveraged to produce a LOGO style language (and execution environment) > while the code being written is actually proper Java, just extending a > prewritten class. .... I would go in a very different direction. Java static methods are a frequent source of problems for new Java programmers, regardless of age. It would be much better to keep them out of the way as long as possible. Patricia |