Results 1 to 5 of 5

Thread: What if we declare the main method as private?

  1. #1
    Join Date
    Feb 2009
    Posts
    4

    What if we declare the main method as private?

    Hi,

    What if we declare the main method as private?

    waiting for your responses!

  2. #2
    Join Date
    Jan 2009
    Posts
    44

    Re: What if we declare the main method as private?

    The program compiles properly but at runtime it will give "Main method not public." message.

  3. #3
    Join Date
    Feb 2009
    Posts
    8

    Re: What if we declare the main method as private?

    If you declare main() as private insted of public, we can compile the program but we cannot get the output.

  4. #4
    Join Date
    May 2008
    Posts
    115

    Re: What if we declare the main method as private?

    main() is declared as public and static because

    main() : public

    it should be accessable by java interpreter .

    if it is declared other than public it cannot be accessible by jvm because

    it is only a packagelevel access.

    main(): static

    this is the method which calls other methods.

    main() method is sharable by all methods & it is not particular to any object or class.

  5. #5
    Join Date
    Jun 2008
    Posts
    97

    Re: What if we declare the main method as private?

    The static modifier means that it does not have to be instantiated to use it. Before a program runs there are technically no objects created yet, so the main method, which is the entry point for the application must be labeled static to tell the JVM that the method can be used without having first to create an instance of that class. Otherwise, it is the "Which came first, the chicken or the egg?" phenomenon. Your main method should be declared as follows:

    public static void main (String[] args) { lots of your java code... }

    hello ,iam Ekavir


    as we know, java is a pure OOP , thats means every thing should be with in the class, main also,because main itself a function, static member functions should not refer through obj of that class. but,we can access static functions through class name itself.. like : class TestMain { public static void main(String args[]) { body; } } now, cmd>javac TestMain.java cmd>java TestMain as we know the static member functions has to call through its class name. thats why the program name must be same as the class name ,where we written the main function,ok!.

Similar Threads

  1. what are the possible return types of a main method?
    By \"Dritan\" in forum Software Development
    Replies: 3
    Last Post: 08-01-2011, 03:50 AM
  2. Explain main method in java
    By Angelica Maria in forum Software Development
    Replies: 5
    Last Post: 10-03-2010, 01:18 PM
  3. Why main method is static in java?
    By MKAIF in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 04:20 PM
  4. How to pass arguments to the main method
    By Wannabe in forum Software Development
    Replies: 3
    Last Post: 12-09-2009, 11:51 AM
  5. When to Declare a static method in JAVA
    By Aamin in forum Software Development
    Replies: 2
    Last Post: 04-02-2009, 07:25 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,653,407,769.65461 seconds with 17 queries