This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between 2 exceptions when main method is not declared properly

 
kunal Agarwal
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the following code...



When I tried running java subclass - I get a proper message saying -
Main method not public


with this code however...


I get an exception -


How these true runtime errors are diffrent? Is it like the first one is caught and properly formatted - and the 2nd one is not...?


 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The signature for the main method is



public means it can be accessed from anywhere, not just within the same folder/package. This modifier is necessary so java can excecute the main method in the "subclass" class.
static means it is a class method (i.e. you don't have to have a "subclass" object before calling this method.)
void means it does not return a value.

In your first error, the static modifier is present so java can see the main method; it just can't execute it because it's in a different package.
In your second error, java can't see the main method at all since it's not static. It wouldn't make a difference in this case if you called it public void main(...) or public void cheese(...).

Note: CamelCaseClassesPlease!
 
kunal Agarwal
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..Thanks for replying...
But that's not what I asked...
What I asked is the difference between those 2 error messages...
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your main class has to be public, static. If its not public compiler tell that it has to be public ( because it sees that there's a static method with name- main ). But in the second case where main method is not static, then its just any other method in the class. Hence compiler complains that it cannot find the main() method. Whereas in the first case the compiler is able to locate the main method but its visibility is not as per the rules.

You can try writing a code with public static main(String[] args) and public main(String[] args).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic