aspose file tools*
The moose likes Beginning Java and the fly likes A question about main method in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A question about main method in java" Watch "A question about main method in java" New topic
Author

A question about main method in java

Phani Kumar
Greenhorn

Joined: Feb 15, 2002
Posts: 22
In general when we write main method,we make it as public static void main.
but instead we can also write private static or protected static.
the code works fine.
please tell me what is the difference among these three?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Unfortunately, the case you describe is special, and you should not use this knowledge to think it works the same with other static methods.
The reason that main() can be private or protected is that the JVM uses this method as the starting point when it executes a java application.
If I have a simple class...
class Foo{
public static void main(String[] args)
{
System.out.println("I am foo");
}
}//end class Foo
and you run this by typing...
java Foo
The JVM will start a new user thread, and begin executing the main() method. Since the main() entry point must always be accessible to the JVM, it ignores the access modifier. So even if the main() method is made private, the JVM will still be able to execute it.
Again, this is a very special exception and you should not think this applies to any other method in java.


Rob
SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question about main method in java