GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes 1 program 1+ main Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "1 program 1+ main" Watch "1 program 1+ main" New topic
Author

1 program 1+ main

Amit Badle
Ranch Hand

Joined: Jan 31, 2001
Posts: 41
hi,
will the following code compile and run??

Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Have you tried it? That's the best way to answer a question like this.
Every class in your system can have a "main" method if you want it to. That's why when you run a Java program you must specify which class to start with - the interpreter runs the "main" method in the named class


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Amit Badle
Ranch Hand

Joined: Jan 31, 2001
Posts: 41
hi,
yes i have tried it and it does compile and run. the question arises that is each class in itself a driver program now ... i dont kno bout lots of other OOPs architecture but is this deliberately a part of the JVM architecture. coz the other langs i kno support only one main. or is it that such a thing does not create any probelms in the design of a program.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Yes, it is intended to be this way. One of the benefits that this can provide is allowing the classes that are not "conceptually" the driver class to still have a main. This main is then loaded with a bunch of stuff to self-test that particular class. Even though you leave it in the production code, it will never get executed because you don't kick off the application using that class.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Amit Badle
Ranch Hand

Joined: Jan 31, 2001
Posts: 41
hi cindy,
ur explanations as always are very helpful. thanx a lot.
 
 
subject: 1 program 1+ main