This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Difference between 2 exceptions when main method is not declared properly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between 2 exceptions when main method is not declared properly" Watch "Difference between 2 exceptions when main method is not declared properly" New topic
Author

Difference between 2 exceptions when main method is not declared properly

kunal Agarwal
Greenhorn

Joined: Aug 15, 2011
Posts: 15
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

Joined: Jul 21, 2011
Posts: 171
    
    5

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!


Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
kunal Agarwal
Greenhorn

Joined: Aug 15, 2011
Posts: 15
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

Joined: Sep 08, 2007
Posts: 3068
    
  33

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).


Mohamed Sanaulla | My Blog
 
 
subject: Difference between 2 exceptions when main method is not declared properly
 
Similar Threads
Junit error
Extends
Exceptions - Basics
Overriding
Overriding a method that throws an Exception.