The static
keyword modifies the lifecycle of a method or variable within a class. A static
method or variable is created at the time a class is loaded. A method or variable that is not declared as static
is created only when the class is instantiated as an object for example by using the new
operator.
The lifecycle of a class, in broad terms, is:
new
operator using the class to make an instance of the class as an actual object and then when done with the object In order to have an initial entry point for an application, Java has adopted the convention that the Java program must have a class that contains a method with an agreed upon or special name. This special method is called main()
. Since the method must exist whether the class containing the main method has been instantiated or not, the main()
method must be declared with the static
modifier so that as soon as the class is loaded, the main()
method is available.
The result is that when you start your Java application by a command line such as java helloworld
a series of actions happen. First of all a Java Virtual Machine is started up and initialized. Next the helloworld.class file containing the compiled Java code is loaded into the Java Virtual Machine. Then the Java Virtual Machine looks for a method in the helloworld
class that is called main(String [] args)
. this method must be static
so that it will exist even though the class has not actually been instantiated as an object. The Java Virtual Machine does not create an instance of the class by creating an object from the class. It just loads the class and starts execution at the main()
method.
So you need to create an instance of your class as an object and then you can access the methods and variables of the class that have not been declared with the static
modifier. Once your Java program has started with the main()
function you can then use any variables or methods that have the modifier of static
since they exist as part of the class being loaded.
However, those variables and methods of the class which are outside of the main()
method which do not have the static
modifier can not be used until an instance of the class has been created as an object within the main()
method. After creating the object you can then use the variables and methods of the object. An attempt to use the variables and methods of the class which do not have the static
modifier without going through an object of the class is caught by the Java compiler at compile time and flagged as an error.
import java.io.*;
class HelloWorld {
int myInt; // this is a class variable that is unique to each object
static int myInt2; // this is a class variable shared by all objects of this class
static void main (String [] args) {
// this is the main entry point for this Java application
System.out.println ("Hello, World\n");
myInt2 = 14; // able to access the static int
HelloWorld myWorld = new HelloWorld();
myWorld.myInt = 32; // able to access non-static through an object
}
}