Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Why main method is public in Java

 
Greenhorn
Posts: 13
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In java if a class is not public and if its members are public then it does not make any sense. (I mean if the class itself is not public then its of no use if members are public)

why this rule not applicable for main() method (public static void main(String a[])).
we are forced to write main method as public.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Noble Bhaskar Aswanapally wrote:In java if a class is not public and if its members are public then it does not make any sense. (I mean if the class itself is not public then its of no use if members are public)



Not true. It's quite common to use private nested classes or package-private classes to implement an interface.

why this rule not applicable for main() method (public static void main(String a[])).
we are forced to write main method as public.



You can define main() however you like it. There's nothing special about main() as far as the Java language is concerned. The only time it matters is if you want to use that class as JVM's entry point to your app.

When you tell the JVM which class to run, it looks for the signature public static void main(String[] args) in that class. That's just a decision the designers made, and it has nothing to do with the Java language itself. It's purely a JVM rule.

And it makes sense to me. If I want, I can define a main() method in my class that is only for my use, and not for use as an entry point to the program by the JVM, simply by making it non-public. For instance:



This way, the outside world can only enter my program through the classes in which I provide a public main(), but I can have those classes to their own independent setup work, and then call a "real" main that kicks off my application subject to the preparation the other classes have done. The outside world can't start my program with that class, so I have control over how it gets started.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's really cool explanation. here I have one more concern(obviously i may miss here something):
why cant the compiler smart enough to make all the methods of an interface to default, if the interface marked as a default one?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:that's really cool explanation. here I have one more concern(obviously i may miss here something):
why cant the compiler smart enough to make all the methods of an interface to default, if the interface marked as a default one?



I don't know why that decision was made. Probably because it would be pointless to do so, and it would add complexity to the language definition and compilers.

If the interface is package-private, nobody else can get to it anyway unless you give it to them, so making the methods package-private doesn't help.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In java if a class is not public and if its members are public then it does not make any sense. (I mean if the class itself is not public then its of no use if members are public)


this bothered me for a little while too. if you implement an interface all those methods are public. many times i have classes that are package access but implement an interface so are forced to have public methods. i finally decided it is not a problem
 
Marshal
Posts: 74371
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is possible to have a package-private interface and use public classes in the same package to implement it.
The concept is that an interface determines the public interface of the class. So, with that idea in mind, you make the methods public.
 
Noble Bhaskar Aswanapally
Greenhorn
Posts: 13
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You can define main() however you like it. There's nothing special about main() as far as the Java language is concerned. The only time it matters is if you want to use that class as JVM's entry point to your app.

When you tell the JVM which class to run, it looks for the signature public static void main(String[] args) in that class. That's just a decision the designers made, and it has nothing to do with the Java language itself. It's purely a JVM rule.

And it makes sense to me. If I want, I can define a main() method in my class that is only for my use, and not for use as an entry point to the program by the JVM, simply by making it non-public. For instance:



This way, the outside world can only enter my program through the classes in which I provide a public main(), but I can have those classes to their own independent setup work, and then call a "real" main that kicks off my application subject to the preparation the other classes have done. The outside world can't start my program with that class, so I have control over how it gets started.



Hi Jeff Verdegan ,
thanks for reply.
The main method here I am speaking about is the main method which will be used by JVM as an entry point to start the execution of program (i.e public static void main(String[] args))
From your reply, I understand that its just a decision that designers have made for the main method to be public. while its really not required.
where as static is required because JVM calls the main() method using class name.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It is possible to have a package-private interface and use public classes in the same package to implement it.
The concept is that an interface determines the public interface of the class. So, with that idea in mind, you make the methods public.


overridden method can have more visibilities
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i believe it is necessary for main to be public. the JVM is certainly not in the same package as your class file

as for the name itself, as others have said, there is nothing special about it. i have used method names like run() before that might confuse someone reading the code.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randall Twede wrote:i believe it is necessary for main to be public. the JVM is certainly not in the same package as your class file


hmm. I can run a package-private class with main!
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's interesting. i have never tried it but i believe you. perhaps it has to do with how we cd to the folder before invoking java or javac
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Noble Bhaskar Aswanapally wrote:
From your reply, I understand that its just a decision that designers have made for the main method to be public. while its really not required.



It is not required, no. They could have decided to allow private main()s to be invoked as well.

However, I don't think it was arbitrary either. As I indicated in my previous post, it makes sense to allow us to create main methods that the JVM can't invoke as the starting point for our app. It makes sense that the JVM should have to follow the same access rules as any other code.

Look at it this way: You can view the JVM as just an already-running Java program that views our class as just another class it's using. At some point, the code in the JVM wants to invoke OurClass.main() and just like if any other Java code were invoking it, it can only do so if it's public.

where as static is required because JVM calls the main() method using class name.



No, that's not why it's static. It's static so that we don't have to "bootstrap" an instance of our class. Our program can start without having to create an instance. I don't think it would have been a big deal to make it so that main was not static, and our program had to have a public no-arg constructor, so that the JVM could create an instance and then call the non-static main() on that instance, but again, the designers chose not to do that. They probably had some reason for it, but I don't know what it was.
 
Noble Bhaskar Aswanapally
Greenhorn
Posts: 13
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You can view the JVM as just an already-running Java program that views our class as just another class it's using. At some point, the code in the JVM wants to invoke OurClass.main() and just like if any other Java code were invoking it, it can only do so if it's public.



Jeff,
if we view JVM as just an already-running Java program that views our class (having main method) as just another class whose main method is to be invoked.
There is small problem.
see below example



if we want to call the main method from a class present out side of this package. we can not because the class is not visible
in the same way the JVM should also fail to invoke, where as in actual case it executes with out fail.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Noble Bhaskar Aswanapally wrote:

You can view the JVM as just an already-running Java program that views our class as just another class it's using. At some point, the code in the JVM wants to invoke OurClass.main() and just like if any other Java code were invoking it, it can only do so if it's public.



Jeff,
if we view JVM as just an already-running Java program that views our class (having main method) as just another class whose main method is to be invoked.
There is small problem.
see below example




I didn't say it was a perfect analogy.

I'm just saying it's a reasonable view for why the main() method has to be public. I can't really imagine why they wouldn't have required the class to also be public though. I would have thought there'd at least be some consistency. It may just be a historical thing that's not worth fixing, or there may actually be some obscure reason for it.

if we want to call the main method from a class present out side of this package. we can not because the class is not visible
in the same way the JVM should also fail to invoke, where as in actual case it executes with out fail.
 
Campbell Ritchie
Marshal
Posts: 74371
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JVM can “see” method of any access type. It is not bound by the syntax rules
 
Ranch Hand
Posts: 30
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why this rule not applicable for main() method (public static void main(String a[])).
we are forced to write main method as public.



1. JVM doesn't care about whether you have a main() or not until you try to run the class itself (doesn't mean instantiating inside anther class).

2. If you really need to run a specific class(often), then you must have a method with "public static void main(String... args)" signature.

3. Any compilable change in the signature above will result in just another method.

4. Therefore, JVM will complain, because main() is the entry point of your class.

In java if a class is not public and if its members are public then it does not make any sense. (I mean if the class itself is not public then its of no use if members are public)


Check out the following situation.
Here you don't need vehicle to be visible outside its package, but needed to pass its members down.

This kind of special situations need default access [It will be more interesting if you make Vehicle as Abstract]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic