Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java General Queries

 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff for providing the clarifications.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Yogesh thats correct.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and you can call accessible static methods of the class, without instantiation.

edit: added 'accessible'
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic