Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-02-2009
Member
 
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!
Reply With Quote
  #2  
Old 25-02-2009
Member
 
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.
Reply With Quote
  #3  
Old 25-02-2009
Member
 
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.
Reply With Quote
  #4  
Old 25-02-2009
Member
 
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.
Reply With Quote
  #5  
Old 25-02-2009
Member
 
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!.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "What if we declare the main method as private?"
Thread Thread Starter Forum Replies Last Post
what are the possible return types of a main method? \"Dritan\" Software Development 3 08-01-2011 03:50 AM
Explain main method in java Angelica Maria Software Development 5 10-03-2010 01:18 PM
Why main method is static in java? MKAIF Software Development 5 06-03-2010 04:20 PM
How to pass arguments to the main method Wannabe Software Development 3 12-09-2009 11:51 AM
When to Declare a static method in JAVA Aamin Software Development 2 04-02-2009 07:25 PM


All times are GMT +5.5. The time now is 09:36 PM.