Prev: in the Observer (Publish/Subscribe) pattern, are notificationsalien method calls?
Next: Using 'final' with local variables
From: Ross on 14 Sep 2009 11:04 On Sep 14, 3:48 pm, Leif Roar Moldskred <le...(a)huldreheim.homelinux.org> wrote: > I'd recommend you make an inherited business method, then write > explicit main methods for all the classes that will use it (There > can't be _that_ many of them, and it make it a lot easier to > understand what's happening.) But, I want to deliver a system where the end user will make up their own classes. Hence i can't pre-write methods without constraining the end user to a relatively short list of possible class names.
From: Mike Schilling on 14 Sep 2009 14:34 If I understand the problem, the user runs java classname where classname has a superclass that defines the main(String[]) method, and you want it to figure out what "classname" is. This can't be done. However, if you change the command to java mainclass classname where classname, instead of subclassing mainclass, implements some known interface, it becomes trivial. (mainclass.main() calls class.forName().newInstance() on its argument, but you knew that.)
From: markspace on 14 Sep 2009 15:25 Ross wrote: > 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 I'm still more than a little confused about what you want the final result to look like. Do you want a command line? An IDE? Something different? > The alternative is to modify the IDE that will be used to supply the > name of the class as the first argument to the running program. This could be done pretty easily in NetBeans by setting the default template for a new class, and turning on code folding. The kids will not see the boilerplate code supplied by the IDE within the code folds. You also might be able to use a custom Ant script. I'm not sure about generating one on a new project though.
From: Daniel Pitts on 14 Sep 2009 17:33 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. You could also have your main in a *different* class than the one being subclassed, and then pass the "Game Class Name" as a separate query argument. Also, It could be a requirement that your Game class does *not* supply a main method, but instead make the "main method" easy to write. -- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
From: Arne Vajhøj on 14 Sep 2009 20:06
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(); Arne |