Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Why main(String args[]) method needs to be public?

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    28
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why main(String args[]) method needs to be public?

    Considering a simple Helloworld program

    class Helloworld{
    public static void main(String args[]){

    System.out.println("This is Hello World Program");

    }

    }

    The above program compiles and runs smoothly.

    The question is I have not declared class public.Yet JVM can access my code and run it down.But if I remove
    pubic modifier from main method signature..........i.e

    class Helloworld{
    static void main(String args[]){

    System.out.println("This is Hello World Program");

    }

    }
    it wont compile.I mean if it can access my class why cannot the main method?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: Why main(String args[]) method needs to be public?

    The simple answer is because that's what the language designers decided. It's probably best to not worry about it too much. I could come up with a few logical reasons, but the simple fact is, that's just how it is.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    28
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why main(String args[]) method needs to be public?

    Quote Originally Posted by KevinWorkman View Post
    The simple answer is because that's what the language designers decided. It's probably best to not worry about it too much. I could come up with a few logical reasons, but the simple fact is, that's just how it is.

    Hey please let me know the reason.....it would be great.......! else this question will start eating my until I am convienced....

    Thanks a lot

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: Why main(String args[]) method needs to be public?

    Quote Originally Posted by rohan22 View Post
    Hey please let me know the reason.....it would be great.......! else this question will start eating my until I am convienced....

    Thanks a lot
    Like I said, it doesn't really matter why. There probably isn't a "this is why main must be public" section of the JLS. Actually, in some of the reading I'm doing, they claim that main doesn't have to be public at all- although my testing does not support that claim.

    Can *you* think of any reasons why it makes more sense for main to be public? Note- Just because a JVM *can* do something, does that mean it *should* do something?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    28
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why main(String args[]) method needs to be public?

    Quote Originally Posted by KevinWorkman View Post
    Like I said, it doesn't really matter why. There probably isn't a "this is why main must be public" section of the JLS. Actually, in some of the reading I'm doing, they claim that main doesn't have to be public at all- although my testing does not support that claim.

    Can *you* think of any reasons why it makes more sense for main to be public? Note- Just because a JVM *can* do something, does that mean it *should* do something?
    If main doesn't work without public then I assume there must be valid reason for it to be public.Something related to its syntax specification.
    Anyways thanks a lot. Like you my search for this is still ON....!.Update me if u find something logical....

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Why main(String args[]) method needs to be public?

    Quote Originally Posted by rohan22 View Post
    If main doesn't work without public then I assume there must be valid reason for it to be public.Something related to its syntax specification.
    Anyways thanks a lot. Like you my search for this is still ON....!.Update me if u find something logical....
    As far as i know, main() is called by JVM and it must has the ability to get seen of outside of the class so it's declared as public.
    Coz, in Java if you don't declare with any access specifier, it's package protected, and your application can contain multiple packages, so it must have to be public to get known and seen by JVM.

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    28
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why main(String args[]) method needs to be public?

    Quote Originally Posted by Mr.777 View Post
    As far as i know, main() is called by JVM and it must has the ability to get seen of outside of the class so it's declared as public.
    Coz, in Java if you don't declare with any access specifier, it's package protected, and your application can contain multiple packages, so it must have to be public to get known and seen by JVM.

    Yea i got your point.....As in java if we don't specify with any access specifier its package protected.I totally agree with you.
    Coming back to my piece of code:-
    class Helloworld{
    public static void main(String args[]){

    System.out.println("This is Hello World Program");

    }

    }

    When JVM can access my class which is package protected(As shown i have not preceded with any access specifier to my class signature),why cannot JVM access my main method without public access specifier.
    Either this code is logically reasonable:- (Will compile and run)

    public class Helloworld{
    public static void main(String args[]){

    System.out.println("This is Hello World Program");

    }

    }

    OR this one: (Will compile but wont run....reason....."Main method not public")
    class Helloworld{
    static void main(String args[]){

    System.out.println("This is Hello World Program");

    }

    }

    But this one(the original one) is bothering me:-
    Bottom line "When JVM can access default/package protected class......why not main(String args[]) method"

    Heys Guys one more thing to add.I have google'd and got to know that in java 1.4 or 1.3 release the main(String args[]) method could have
    been declared even private and protected but was later changed in java's subsequent versions......Its just an information exchange.
    Lets not change the bottom line of my question.

    Thank You!

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Why main(String args[]) method needs to be public?

    Bottom line "When JVM can access default/package protected class......why not main(String args[]) method"
    JVM never knows if you have multiple packages in your application or not. So i guess, logically it actually gives run time error only to avoid later run time errors (if there will be multiple packages).

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: Why main(String args[]) method needs to be public?

    Quote Originally Posted by rohan22 View Post
    I have google'd and got to know that in java 1.4 or 1.3 release the main(String args[]) method could have
    been declared even private and protected but was later changed in java's subsequent versions.
    That makes sense. Guess the examples I saw of making main private were older, which is why they don't work on my JVM.

    Quote Originally Posted by Mr.777 View Post
    JVM never knows if you have multiple packages in your application or not. So i guess, logically it actually gives run time error only to avoid later run time errors (if there will be multiple packages).
    I'm not sure how packages impact the scope of main. Sure, it can have package scope, but that wouldn't lead to runtime errors (in fact, as the OP pointed out, the requirement that main be public is relatively new to the language). His question is, why did Java make the switch?

    I did some more research, and this is the brunt of my findings: Cafe au Lait Special Report: Non-public main() method

    Apparently, Java originally required main to be public, as it does now. Then there were a few versions that did not enforce that rule, which may or may not have been a bug. For more info, check out the discussions in this bug report: Bug ID: 4252539 private void main(String[] args) not checked by jdk1.2 vm

    And although there is no "why" section, the JLS does indeed require that main be public: Execution "The method main must be declared public, static, and void."

    I hope that helps. But the answer to your original question of "why" is still mostly "because that's the rules". We could come up with logic saying that letting private methods be available violates OOP principles on which Java is founded, or that forcing main to be public makes it easier to do testing, compounding, and scripting of programs. Or we could come up with logic arguing that main should be allowed to be private. Either way, it doesn't matter. It is the way it is. It's like saying, why is ArrayList called ArrayList and not ListArray? That's just what the language designers decided. There might be logic behind it, and there might be arguments against it, but it doesn't change the fact of the matter.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Junior Member
    Join Date
    Jul 2011
    Posts
    28
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why main(String args[]) method needs to be public?

    Quote Originally Posted by KevinWorkman View Post
    That makes sense. Guess the examples I saw of making main private were older, which is why they don't work on my JVM.



    I'm not sure how packages impact the scope of main. Sure, it can have package scope, but that wouldn't lead to runtime errors (in fact, as the OP pointed out, the requirement that main be public is relatively new to the language). His question is, why did Java make the switch?

    I did some more research, and this is the brunt of my findings: Cafe au Lait Special Report: Non-public main() method

    Apparently, Java originally required main to be public, as it does now. Then there were a few versions that did not enforce that rule, which may or may not have been a bug. For more info, check out the discussions in this bug report: Bug ID: 4252539 private void main(String[] args) not checked by jdk1.2 vm

    And although there is no "why" section, the JLS does indeed require that main be public: Execution "The method main must be declared public, static, and void."

    I hope that helps. But the answer to your original question of "why" is still mostly "because that's the rules". We could come up with logic saying that letting private methods be available violates OOP principles on which Java is founded, or that forcing main to be public makes it easier to do testing, compounding, and scripting of programs. Or we could come up with logic arguing that main should be allowed to be private. Either way, it doesn't matter. It is the way it is. It's like saying, why is ArrayList called ArrayList and not ListArray? That's just what the language designers decided. There might be logic behind it, and there might be arguments against it, but it doesn't change the fact of the matter.

    Awesome..... thanks a lot.....But we are back to the square in giving the answer....."the JLS says so....".Yes i agree not much information
    is available on net on this topic.But will keep the thread alive.If anyone of us gets to something interesting information about it,
    lets keep updating.

    Happy coding !
    Cheers.

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: Why main(String args[]) method needs to be public?

    I'm all for that, but I really doubt there's a definitive "this is why things are the way they are" source of information. We can come up with reasons that forcing main to be public makes sense, and we can come up with reasons that not doing so makes sense too. In the end, the language designers had to choose one. The fact that one of the releases did not enforce it would appear to be a bug, since it has been "fixed" in subsequent releases.

    Also, there is a chance that this is implementation dependent. Perhaps some JVMs require main to be public and some do not. This would seem to violate the JLS, but different people can interpret the same sentence differently.

    In the end, it's just one of those things that's simply true. Imho, your time is better spent elsewhere, such as writing code or reading tutorials, than it is spent picking apart the thought processes of the language designers.

    Or you could email James Gosling and see whether he replies, haha.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Why main(String args[]) method needs to be public?

    I'm not sure how packages impact the scope of main. Sure, it can have package scope, but that wouldn't lead to runtime errors (in fact, as the OP pointed out, the requirement that main be public is relatively new to the language). His question is, why did Java make the switch?
    His question was later about java switch, but the actual question is why if not provided any access modifier to main() generates run time error?

    And i guess yes, package scope matters alot. If we can say that it is specified in the JVM, so this can be the one of the reasons, (not very sure but still makes sense).

Similar Threads

  1. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  2. public static void main(NewMember Mr_Bukkake){ }
    By Mr. Bukkake in forum Member Introductions
    Replies: 2
    Last Post: October 13th, 2011, 07:29 AM
  3. Java main method args
    By mattxo in forum Java Theory & Questions
    Replies: 3
    Last Post: May 16th, 2011, 09:45 AM
  4. Error using public static double Method
    By stommy989 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 13th, 2010, 03:01 PM
  5. Convert string to private and public key
    By Ganezan in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: January 5th, 2010, 07:37 AM