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 4 of 4

Thread: Problem in understanding the declaration of main.

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem in understanding the declaration of main.

    Why main is declared as public in its declaration.
    public static void main(String [] args)
    {
    // code
    }

    plz. help me out.


  2. #2
    Member mjr's Avatar
    Join Date
    Jan 2012
    Location
    127.0.0.1
    Posts
    36
    My Mood
    Fine
    Thanks
    8
    Thanked 2 Times in 1 Post

    Default Re: Problem in understanding the declaration of main.

    Have you tried declaring it private to see what happens?

    I'm still fairly new to Java, but remember: main() is inside a class. The class that holds main() is public. And remember that if a method is private, it can only be called from within the class itself.

    Try changing your declaration to this:

    private static void main(String[] args){
    // code
    }

    and see what happens...

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Problem in understanding the declaration of main.

    Actually, some JREs allow main to be private. It's a weird quirk, so don't rely on it.

    But what do you mean "why is main declared public"? It just is. That's the rule (except for the weird quirk part). It makes sense that it's public, so other things (including the JRE) can have access to it. Otherwise your program couldn't run.

    But really, just accept it and move on. It's just a waste of time to think too hard about "why is this named that" in a particular language- it just 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!

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem in understanding the declaration of main.

    Quote Originally Posted by KevinWorkman View Post
    Actually, some JREs allow main to be private. It's a weird quirk, so don't rely on it.

    But what do you mean "why is main declared public"? It just is. That's the rule (except for the weird quirk part). It makes sense that it's public, so other things (including the JRE) can have access to it. Otherwise your program couldn't run.

    But really, just accept it and move on. It's just a waste of time to think too hard about "why is this named that" in a particular language- it just is.
    Thanks a lot for your sincere help kevin

Similar Threads

  1. problem in understanding output.
    By vinaysa86 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 5th, 2012, 01:22 AM
  2. What is the problem with this following declaration in JDK 1.4?
    By Senjuti in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 2nd, 2011, 11:37 AM
  3. Problem understanding Java code modification
    By eagle09 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 26th, 2011, 04:13 PM
  4. main methos problem
    By bondage in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 1st, 2010, 02:43 AM
  5. Main Class Not Found Problem
    By shadow in forum Java Theory & Questions
    Replies: 3
    Last Post: September 29th, 2009, 09:42 AM