• Post Reply Bookmark Topic Watch Topic
  • New Topic

main method  RSS feed

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


output:
a = 42
b = 99


These classes are running in one package, then every class in java can have its own main method, and their main method will run and compile without any problem , and it is not necessary to specify main method,Right?
 
Paul Witten
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:These classes are running in one package, then every class in java can have its own main method, and their main method will run and compile without any problem , and it is not necessary to specify main method,Right?

You can have a main() in every class but it's not necessary. If Class A wants to reach into Class B it only needs to initialize an instance of B. If B has no main() then that is not going to stop A from getting an instance of B and reaching it's members or methods.

If there is a main() in B it will not run (to my knowledge) automatically if A invokes an instance of B. You need to invoke B.main() directly if class A main() was the entry point to the package. Since main() is public static you should have no problem running it directly if it's in your package.




 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only class that needs a main method is the one that serves as an entry point to your program for the JVM. The one that kicks off everything else.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!