Link to home
Start Free TrialLog in
Avatar of anmadhu
anmadhu

asked on

Why should be declared as static in main?

In java (standalone) pgm must have one main function.
the line should be
public static void main(String a[]){}
why should pu static in main()? what is the advantage adn what is the reason?
..
thanking you experts
ASKER CERTIFIED SOLUTION
Avatar of Venci75
Venci75

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Neutron
Neutron

The designer of the Java-Application concept probably had the following alternatives:

- to have a standard constructor for Java-Application, something like:

class MyClass
{
    public MyClass( String[] args )
    {
    }
}

- or to have a parameterless constructor and some init method:

class MyClass
{
    public MyClass()
    {
    }

    public void init( String[] args )
    {
    }
}

- it seems that the best option is the chosen one, because JVM doesn't need an instance of your 'main' class in order to run it, so you can decide whether you will create an instance or not.

This info is for free :-)

Greetings,
    </ntr> :)
main method is the starting point of a java program. When you execute a java program, you execute something like this:
java MyClass
JVM will invoke MyClass.main(param)

In order to let JVM do so, main method must be:
1. Public, so that it can be invoked from ourside of the package, for example, if your MyClass is in packet com.myjava.example, JVM still can invoke it.
2. Static, so that JVM doesn't need to create an instance of MyClass. To create an instance, usually JVM must call the constructor of that class, but it doesn't know which parameter to pass to the constructor. So JVM cannot create an instance of MyClass and then call the starting function. So main must be static (can be called alone).
hope this helps
A good pattern to use when writing a Java application is to have the public static main() method create an instance of the object, then run one or more methods. The main() method just checks the arguments, such as if the correct number of arguments are present.

For example, let's say I make a Java class that copies a file. I decide that the application should take two arguments, one as a source and one as a destination.

public class Copy {
  // This method does the actual work of the application
  public void copy( String source, String destination)
      throws IOException {
    ...
  }
  public static main( String[] args) {
    // First check that the right number of arguments were given
    if( args.length != 2) {
      System.err.println( "Usage: " + class.getName() + " source destination);
      System.exit( 1);
    }

    // Now make an object to perform the copy.
    Copy myCopyApplication = new Copy();
    myCopyApplication.copy( args[ 1], args[ 2]);
  }
}

This code won't quite compile because I didnt put all the exception catching (and because I typed it out of my head without ever compiling it).

But if you do it this way, you now have a reusable copy class that can be used from other classes. Another object which needs to copy things can just do

    Copy myCopyHelper = new Copy();
    myCopyHelper.copy( source, destination);
Hi
   we can directly access the object without creating an instance and it contain global variables.
 
 


Many of these comments have the answers themselves.
But still i would like to explain u in detail.

Java is designed in a way thatall standalone programs nedd to be started at one point (means to be loaded to the memory).
to run a program we say java <program Name>. we have not created the instance or ur javafile yet. then with what referrence the JVM will load the program to memmory. To Acces a variablw we need its claas object which will be created only if it is instantiated. So, we mark main() as static, saying it can be accessed directly with the program name.
i.e.,  our JVM will locate the main method with the program name supplied by us in this manner:

      <programName>.main();
if no main method is found in ur class, it will throw u no defn found error.

so, all the variables, that u want to access before instantiating the class, can be marked as static and it can be accessed by u with its program name like this:

    <programName>.<variable>;
one good example for static is singleton class.
where u will have static variable holding ur classobject. u will acess it with classname, it will return theclassobject to u, inorder to maintain the single version object of that class throught out ur program.

I'm typing this message in a hurry. So, if there are any mistakes pls., ignore it.

Regards,
K.J.S.
hia friend ,

   before telling the answer . i would like to teel one thing that java is a pure object oriented language . we call a pure oops if every thing is included in the class .
so we have the main function inside the class . but it should be the only one for all the objects so we be keeping as a static so that only one copy is maintained for all the objects .it made public so that it can accessed out side the class

 hope you got the answer .
bye
sudhir(MSIS)
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- points to Venci75

All experts have replied with the correct comments, hence I am requesting it to be awarded to the one who answered first

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
Comment from expert accepted as answer

Computer101
E-E Admin