This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Java General Queries Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java General Queries" Watch "Java General Queries" New topic
Author

Java General Queries

Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
I have a few general queries about java:

i. Why the class should be public having main method?

ii. Why the class name must be the same as the file name?

iii. Why the main method return type is void?

iv. Why the main method is static? Why the JVM itself can't create an object of our class and could have called the main method?

v. Why the main method is public and not protected/default?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

VaibhavG Garg wrote:I have a few general queries about java:

i. Why the class should be public having main method?


Because that was what the designers decided upon. Unless we ask them directly, or get hold of their notes, we have no way to know for sure. We can speculate however, that it makes sense that, since the entry point to an app comes from outside that app, that it should only be allowed if we explicitly state that it is allowed (by declaring that class public).

ii. Why the class name must be the same as the file name?


Probably for simplicity and clarity of organization.

iv. Why the main method is static? Why the JVM itself can't create an object of our class and could have called the main method?


How would it know which constructor to call and what arguments to pass it? Sure, rules and conventions could have been decided upon, but they'd be somewhat arbitrary. It's simpler just to make it static, and leave it up to us to create whatever objects we need there.

v. Why the main method is public and not protected/default?


Same answer as the first one.

In the future, please SearchFirst.(⇐click)
Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
Thanks Jeff for providing the clarifications.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
VaibhavG Garg wrote: . . . i. Why the class should be public having main method? . . .
Do you mean you can only write a main method in a public class? That is mistaken.
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 207
i think what he is trying to ask is

a class that can be executed should be public....why so....if i understand it correctly..
Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
Yes Yogesh thats correct.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
Yogesh Gandhi wrote:i think what he is trying to ask is

a class that can be executed should be public....why so....if i understand it correctly..
I think you have understood it correctly. But it is still mistaken. You can’t execute a class; you can instantiate it and execute its methods. And you can instantiate and call methods on private public protected or package-private classes.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

... and you can call accessible static methods of the class, without instantiation.

edit: added 'accessible'


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java General Queries
 
Similar Threads
class name
Wrong coding
A question about the java programming rule!
about main method
name of java file