Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

1 program 1+ main

 
Amit Badle
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
will the following code compile and run??

 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Amit Badle
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amit Badle
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi cindy,
ur explanations as always are very helpful. thanx a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic