Use of Static

What is the use of static in (public static void main string args[])

Questions by anandkumarjha   answers by anandkumarjha

Showing Answers 1 - 30 of 30 Answers

geek_ani

  • Aug 11th, 2010
 

Primarily so that the JVM doesn't have to instantiate your controlling class to get an entry point into the program. With a static main, all you need to know to invoke a Java program is the name and location of a class. If it wasn't static, you'd also have to know how to instantiate that class, or require that the class have an empty constructor.

  Was this answer useful?  Yes

In classes, a static method is one that is called without an instance of that class, while a non-static method is called by instances of the class. main is the first method to be called in java program. main method should be called before any instance of its class is created. Therefore we use static in main method.

bhrigu 007

  • Aug 18th, 2010
 

Our main function is static so that main can call other static function .. as static function can be called by other static function.

  Was this answer useful?  Yes

Static: It is a keyword. When a member is declared as static, it can be
accessed before any object of its class are created and without reference to any
object. You can declare both methods and variables to be as static.


Example1: main() is declared as static and it must be called before any
object exist. These static variables can behave as a global variable as exist
individually with all the instances of a class.


Example 2: class Cal
{
Statc void area(int l,int b)
{
int r= l*b;
System.out.println("Area="+r);
}
}
class Demo
{
public static void main(String args[])
{
cal.area(2,3);
//Classname.methodname(parameter)


}
}

OUTPUT:-Area=6
In public static void main() static keyword is used to maintain all the class
variables global.


  Was this answer useful?  Yes

pawansahu

  • Sep 9th, 2010
 

The static member can be shared by many objects. means many instance of class can work on this shared data.
Let we want to make the program of object counting then we will make a static variable and it will take default value automatically zero. Then in constructor we will give increment in that variable. Thus we can count the no. of objects.

  Was this answer useful?  Yes

Ganesh MN

  • Jan 6th, 2011
 

Static keyword is used to create methods and variables that exist independently of any of the instances of the enclosing class. i.e only one copy of static variable/method will be available in the memory and is shared by all the instances created for tat class and they can be accessed directly using corresponding  class name without creating objects of that class.

  Was this answer useful?  Yes

For example if we want to access the data of an object is like this first we create an object and then by using the object reference we can access the methods in that class.
For example if any person ask's
you with out creating the object can u able to access the methods then you can say yes because if
you declare the methods in that class as static then by using the class name itself we can access those methods.

Here in your question why static is used in public static void main(String args[]).
Generally we write one class in that class one of the method is declared as public static void main(String args[]) and by using javac command we can compile that class.  After that by using java command we can run the java program.  Ok consider JVM is also one type of java program which is written by java people. 
When you run the program u said to the JVM run my program. Ok JVM is a java class as well as
your program is also a Java class so JVM class has to access the main method of
your program but how. So the answer for this problem is when at time of run we give the java command as like this

ex::java MainTest

Here java is command for jvm and MainTest is our class so jvm will take the MainTest class name and it calls the main program in our class as given below

MainTest.main(String args[]);

because main(--) method is declared as static using the class name itself
JVM will access the method.

  Was this answer useful?  Yes

Suresh

  • Aug 6th, 2011
 

without creating instance of that class to invoke the main method static keyword is used, by giving the name of the class main method gets invoked.

  Was this answer useful?  Yes

We can use static key word for any method which does not require an object to call, so main method is automatically called by the JVM so it does not require any object it is mentioned as static. We can call all the static methods from main with out creating object. But we need object for methods which are created without using static keyword.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions