Solved

Why should be declared as static in main?

Posted on 2002-06-25
9
273 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:anmadhu
9 Comments
 
LVL 9

Accepted Solution

by:
Venci75 earned 100 total points
ID: 7106702
because the static methods/variables are global. To execute this method you don't need to have an instance of the class, containing the main() method
0
 
LVL 4

Expert Comment

by:Neutron
ID: 7107664
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> :)
0
 
LVL 3

Expert Comment

by:daitt
ID: 7107831
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
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 3

Expert Comment

by:mjzalewski
ID: 7109372
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);
0
 

Expert Comment

by:rhariss
ID: 7109640
Hi
   we can directly access the object without creating an instance and it contain global variables.
 
 
0
 
LVL 1

Expert Comment

by:saurav17
ID: 7109797


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.
0
 

Expert Comment

by:sudhir_13
ID: 7139629
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)
0
 
LVL 5

Expert Comment

by:vemul
ID: 7888002
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
0
 
LVL 1

Expert Comment

by:Computer101
ID: 7959667
Comment from expert accepted as answer

Computer101
E-E Admin
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question