wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes why we always declare main method public ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why we always declare main method public ?" Watch "why we always declare main method public ?" New topic
Author

why we always declare main method public ?

Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

why we always declare main method public ?
i am a newbie so please explain it properly.


OCPJP 6 86%
Bill Johnston
Ranch Hand

Joined: Nov 17, 2005
Posts: 201
Nikhil Sagar wrote:why we always declare main method public ?
i am a newbie so please explain it properly.


Well for one thing you won't be able to run the app if it's not public; though you can compile the code. May I ask you why you might want to declare it otherwise?


~Bill
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Nikhil Sagar wrote:why we always declare main method public ?


Because the rules of the JVM say that when it launches your program, it will look for a public main method in the class you give it.

The JVM could have been designed to not care if the method was public, but it's more consistent with the general use and purpose of the access levels. Whatever is invoking your main is not part of the same class or package, nor is it a subclass, so it makes sense that it should only invoke a public main method. Also, this lets you write a non-public main (though I wouldn't do it) and not have to worry that it will be used as an entry point to your program when it shouldn't be.

In the end, though, the real answer, as with all "Why is Java this way?" questions, is, "Because that's what the designers decided on." Unless we get hold of a white paper or their meeting minutes, we can't know what was in their minds. All we can do is examine pros and cons and take an educated guess that they saw the same pros and cons, and made their decision based on those pros and cons, and on what fits best with Java's overall goals, and on what they had the resources to implement in the schedule they were working with.
Amit Goda
Ranch Hand

Joined: Jan 21, 2012
Posts: 37

Just try using private or protected access specifier and you'll get to know what happens.

Try searching on Google for the exception it throws.

Just telling you because its better way to learn. You'll find your answers yourself and your concept will be more clear.


AMiT
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Bill Johnston wrote:
Nikhil Sagar wrote:why we always declare main method public ?
i am a newbie so please explain it properly.


May I ask you why you might want to declare it otherwise?


Just for knowledge.

Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Amit Goda wrote:Just try using private or protected access specifier and you'll get to know what happens.

Try searching on Google for the exception it throws.




I tried using default access modifier and neither it throws any exception nor error.
what i found is a output " MAIN METHOD IS NOT PUBLIC "

Just telling you because its better way to learn. You'll find your answers yourself and your concept will be more clear.

I also tried google but i did not find any satisfactory answer.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Jeff Verdegan wrote:
Nikhil Sagar wrote:why we always declare main method public ?


Because the rules of the JVM say that when it launches your program, it will look for a public main method in the class you give it.

The JVM could have been designed to not care if the method was public, but it's more consistent with the general use and purpose of the access levels. Whatever is invoking your main is not part of the same class or package, nor is it a subclass, so it makes sense that it should only invoke a public main method. Also, this lets you write a non-public main (though I wouldn't do it) and not have to worry that it will be used as an entry point to your program when it shouldn't be.

In the end, though, the real answer, as with all "Why is Java this way?" questions, is, "Because that's what the designers decided on." Unless we get hold of a white paper or their meeting minutes, we can't know what was in their minds. All we can do is examine pros and cons and take an educated guess that they saw the same pros and cons, and made their decision based on those pros and cons, and on what fits best with Java's overall goals, and on what they had the resources to implement in the schedule they were working with.


I love you Jeff, You Rocks.
Bill Johnston
Ranch Hand

Joined: Nov 17, 2005
Posts: 201
Nikhil Sagar wrote:
Bill Johnston wrote:
Nikhil Sagar wrote:why we always declare main method public ?
i am a newbie so please explain it properly.


May I ask you why you might want to declare it otherwise?


Just for knowledge.



That's a good reason. I just wondered is you thought there was some need for another type of access. Jeff's answer was very clear, I agree.
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Okay, I followed this up! I got a little doubt.

Lets use a class with default access.
Inside that, a normal main method which is public. Why?
When the class is default itself, then it should not matter if the methods inside are default or public, they are visible within that package only (considering what we do).

Why and/or how JVM uses it (from any other class ), I think there might lie the answer?

Edit: Yes, I know that JVM is designed to use it that way.


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rajdeep Biswas wrote:Okay, I followed this up! I got a little doubt.

Lets use a class with default access.
Inside that, a normal main method which is public. Why?


I don't know. You tell me. You're the one that came up with the scenario.

When the class is default itself, then it should not matter if the methods inside are default or public, they are visible within that package only


False. You can use public methods of non-public classes anywhere. Even public methods of private classes can be used anywhere.

Why and/or how JVM uses it (from any other class ), I think there might lie the answer?


What are you talking about? Are you asking what the JVM will do if we try to use a non-public main class with a public method as our program's entry point? I don't know, and I can't imagine why anybody would care. It's a pathological corner case, and knowing the answer provides no practical knowledge and no insight into any specific design principles of the JVM spec or implementation. That's one of those things that's so completely insignificant that it just ended up being whatever was easiest or most natural after all the important stuff was worked out.
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Hi,
Jeff Verdegan wrote:You can use public methods of non-public classes anywhere.

Considering a default class, we can not use it outside the package. Now while using it inside that package, the access boundations and rules are same for both its default and public methods. Aren't they?

So my Question: main() is the entry method for any Java application/program. Why they added one keyword extra "public" as we can also have default classes with public methods (and making them public does not matter as informed above)?
Jeff Verdegan wrote:Even public methods of private classes can be used anywhere.

Give an example please!

Jeff Verdegan wrote:What are you talking about? Are you asking what the JVM will do if we try to use a non-public main class with a public method as our program's entry point?

My apologies if I was ambiguous. I know the signature is already specified and you play with it, JVM will never find that entry-point. My question is above again. Why public and not default?
I know that JVM is designed like that, but I am expecting what can be the reasons.

Thanks
Bill Johnston
Ranch Hand

Joined: Nov 17, 2005
Posts: 201
For the reason why Java was designed that way, you might be better off doing some searches on that, unless someone here happens to know the answer already.

But as to what you stated about accessing friendly and public methods in the same package in the same way, then yes, you can. But of course it matters outside the package and that is the point, isn't it?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
What’s a friendly method? I thought you only had them in C++.
Bill Johnston
Ranch Hand

Joined: Nov 17, 2005
Posts: 201
Maybe they don't call default method access that anymore, but back in the day they did.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rajdeep Biswas wrote:Hi,
Jeff Verdegan wrote:You can use public methods of non-public classes anywhere.

Considering a default class, we can not use it outside the package.


Not exactly true. And we can definitely use its public methods outside the package.

So my Question: main() is the entry method for any Java application/program. Why they added one keyword extra "public" as we can also have default classes with public methods (and making them public does not matter as informed above)?


Same reason as stated above for public methods. And note that a public method in a default accessible class is NOT the same as a public method in a public class. And, again, this is a corner case, and it has no bearing whatsoever on any Java code anybody would reasonably ever write, and the only way to know the answer for sure to a "Why is Java this way?" question is to ask the original designers.

Jeff Verdegan wrote:Even public methods of private classes can be used anywhere.

Give an example please!


java.util.AbstractList has:


Whenever we use a LinkedList's or ArrayList's Iterator, we're using an instance of that private class, and we're calling it's public hasNext() and next() methods.

Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Thanks Jeff and others. I will clarify myself more on the last part: Private class with public methods.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rajdeep Biswas wrote:Thanks Jeff and others. I will clarify myself more on the last part: Private class with public methods.


What about it?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7700
    
  20

Jeff Verdegan wrote:False. You can use public methods of non-public classes anywhere. Even public methods of private classes can be used anywhere.

Blimey. Disagreeing with you twice in one day...must be a record.

@Rajdeep: What Jeff says is technically correct; however, since a private class must be nested, ALL its methods and fields are available to its enclosing class anyway, so it really makes very little difference.

If a class is package-private, then it is only visible by things in its package, which again restricts the overall view of things.

Where it is important is if you create an ANONYMOUS class (which you can do from a private nested class). However, the chances are that it will be returned to you in the form of a public interface.

So: when determining the visibility of a method, you need to look at more than one thing:
  • Its definition.
  • The visibility of the class that defines it.
  • The visibility of any interfaces that the class that defines it implements.
  • If ANY of those is public, then the method itself will be publicly visible.

    HIH

    Winston


    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Winston Gutkowski wrote:
    Jeff Verdegan wrote:False. You can use public methods of non-public classes anywhere. Even public methods of private classes can be used anywhere.

    Blimey. Disagreeing with you twice in one day...must be a record.

    @Rajdeep: What Jeff says is technically correct; however, since a private class must be nested, ALL its methods and fields are available to its enclosing class anyway, so it really makes very little difference.

    If a class is package-private, then it is only visible by things in its package, which again restricts the overall view of things.

    Where it is important is if you create an ANONYMOUS class (which you can do from a private nested class). However, the chances are that it will be returned to you in the form of a public interface.

    So: when determining the visibility of a method, you need to look at more than one thing:
  • Its definition.
  • The visibility of the class that defines it.
  • The visibility of any interfaces that the class that defines it implements.



  • I would add:
  • The visibility of the class's superclasses (same issue as visibility of any interface it implements).


  • If ANY of those is public, then the method itself will be publicly visible.

    HIH

    Winston


    Not sure where the disagreement is...
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7700
        
      20

    Jeff Verdegan wrote:Not sure where the disagreement is...

    Probably none. I guess I'm just trying to stress that it's the visibility of the container that's important when you're trying to determine overall visibility.

    Winston
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Winston Gutkowski wrote:
    Jeff Verdegan wrote:Not sure where the disagreement is...

    Probably none. I guess I'm just trying to stress that it's the visibility of the container that's important when you're trying to determine overall visibility.

    Winston


    Container? You mean the enclosing class in the case of nested classes?

    I don't know if that's the case (and am too lazy to experiment just now). I would guess, however, that if I have a package-private top-level class with a private nested class which in turn contains another private nested class, and that class implements, say, Comparable, that I could sort a List of that most deeply nested private class. As long as the reference to my otherwise invisible class becomes available--say by passing as a parameter--I should be able to call its public methods (casting to the appropriate type first, if needed).
    gurpeet singh
    Ranch Hand

    Joined: Apr 04, 2012
    Posts: 924
        
        1

    Winston Gutkowski wrote:
    Jeff Verdegan wrote:False. You can use public methods of non-public classes anywhere. Even public methods of private classes can be used anywhere.

    Blimey. Disagreeing with you twice in one day...must be a record.

    @Rajdeep: What Jeff says is technically correct; however, since a private class must be nested, ALL its methods and fields are available to its enclosing class anyway, so it really makes very little difference.

    If a class is package-private, then it is only visible by things in its package, which again restricts the overall view of things.

    Where it is important is if you create an ANONYMOUS class (which you can do from a private nested class). However, the chances are that it will be returned to you in the form of a public interface.

    So: when determining the visibility of a method, you need to look at more than one thing:
  • Its definition.
  • The visibility of the class that defines it.
  • The visibility of any interfaces that the class that defines it implements.
  • If ANY of those is public, then the method itself will be publicly visible.

    HIH

    Winston


    how can you access public methods of non-public class anywhere? how is it accomplished ?
    Seetharaman Venkatasamy
    Ranch Hand

    Joined: Jan 28, 2008
    Posts: 5575

    gurpeet singh wrote:
    how can you access public methods of non-public class anywhere? how is it accomplished ?

    may be like this, but again why do we do that is question ;)
    pratik gaurav
    Greenhorn

    Joined: Oct 04, 2012
    Posts: 16

    the main method is public to make it accessible to every code written in your package or outside of the package or anywhere
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    gurpeet singh wrote:
    how can you access public methods of non-public class anywhere? how is it accomplished ?


    It's accomplished by the JLS saying it is allowed and the compiler and JVM being written to comply with those rules.

    If a non-public class, PRI, implements a public interface or extends a public class, PUB, then if you have a reference to an instance or PRI that is declared as type PUB, you can access all the public methods that PUB exposes.

    For a trivial case, imagine you have a private nested class, and you go to print out an instance of that class:


    obj is a reference of type Object, but it points to an instance of the private class Inner. When we call println(), that will untimately call Inner's public toString() method. Main's code can't refer to the class Inner by name, but that doesn't prevent it from getting a reference to an instance and calling its public methods.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Seetharaman Venkatasamy wrote:
    gurpeet singh wrote:
    how can you access public methods of non-public class anywhere? how is it accomplished ?

    may be like this, but again why do we do that is question ;)


    That doesn't really demonstrate this issue though. That's just a public method referencing one of that class's own private members. Nobody outside this class is directly calling private class Helper's public methods.
    Seetharaman Venkatasamy
    Ranch Hand

    Joined: Jan 28, 2008
    Posts: 5575

    Thanks Jeff, wonderful example
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7700
        
      20

    Jeff Verdegan wrote:I would guess, however, that if I have a package-private top-level class with a private nested class which in turn contains another private nested class, and that class implements, say, Comparable, that I could sort a List of that most deeply nested private class.

    Right, but that's because the class implements a public interface, which is similar to the classes returned by the Collections "unmodifiable" methods: private nested classes, exposed via public interfaces.
    Your other example with println() is much closer to the mark and, I have to admit, one I've never really thought about. I take it all back, O great Swami, and grovel in humble apology.

    Winston
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Winston Gutkowski wrote:
    Jeff Verdegan wrote:I would guess, however, that if I have a package-private top-level class with a private nested class which in turn contains another private nested class, and that class implements, say, Comparable, that I could sort a List of that most deeply nested private class.

    Right, but that's because the class implements a public interface, which is similar to the classes returned by the Collections "unmodifiable" methods: private nested classes, exposed via public interfaces.


    Yup. Which was exactly my point all along.

    Your other example with println() is much closer to the mark and, I have to admit, one I've never really thought about. I take it all back, O great Swami, and grovel in humble apology.


    We are pleased. You may now fetch us our royal beer.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: why we always declare main method public ?