Static Methods and Variables


Variables and methods of the Student class which we have written can be accessed only after creating an object of that class. Write a test program with a main method containing the following line.
Student.printDetails();
You will receive a compilation error saying that a non static method cannot be referenced from a static context. This is what we are going to deal with static and non static variables and methods.
When a variable or a method is not defined used the static keyword, it cannot be accessed by specifying the class name and the dot operator followed by the variable or method name. This is because such items are said to belong to objects which would be instantiated from those classes. Every object would have its own copy of those variables. The class as such doesn’t possess that particular variable. On the other hand, when something is defined as static, the particular entity, be it a method or a variable belongs to the class itself. There would be only one copy of that static member and all the objects would be sharing it. These static members can be accessed directly without creating an object. We have use the static method main a number of times in our program. Recall that we have never created the ‘HelloWorld’ object but still the program had run. This is because the method main() was declared static. We shall now take a deeper look at static variables and static methods.
Static variables
A variable can be declared as static by using the static keyword in the following way.
static int a;
Such variables can be defined only in a class. The keyword static cannot be used for variables that would be declared in methods. Further, a static variable is set to its default value. There is no need to explicitly specify it. However, we are free to initialise it with some other value if we wish to do so.
static int a = 34;
These static members can be accessed through the class as well as through objects created from the class. Further, there would be only one copy of static variables. All objects would be sharing the same copy. The following program illustrates the use of static variables through the use of a class Number which contains a static variable x and a non static variable y.
public class Number {
static int x; // x will be initialized to its default value 0
int y; // non static variable
Following is the test class.
public class Test {

Public static void main ( String args[] ) {
System.out.println ( Number.x );
Number.x=34; // accessing static variable through class name

Number num1 =new Number ();
num1.y=3;
System.out.println ( num1.x );
System.out.println( num1.y );
num1.x = 347; // accessing static variable through an object
System.out.println(Number.x);

Number num2 = new Number ();
num2.y=4;
System.out.println ( num2.x );
System.out.println( num2.y );
num2.y=3479;

// The three statements below print the same value as there is only one copy of a static variable which is shared by all the objects
System.out.println ( Number.x );
System.out.println ( num1.x );
System.out.println ( num2.x );
}
}

The output would be:
0
34
3
347
347
4
347
347
347
The above program shows that a static variable can be accessed by using the class name, or through an object of that class. Also, there is only one copy of the static variable. That was the reason why the change made through Number.x was reflected on the x that was accessed through num1 also. Further, the static variable was set to zero by default as is evident in the first line of output.
Static methods
Similar to static variables, we have static functions also. Functions or methods are declared as static by specifying the static keyword. A static method can be called either using the class name or the object name. A static function can access only other static members (variables or methods). Attempting to access non static members will result in errors.
Following program shows the usage of static functions.
public class Number {

Static int x = 7;
int y;

Public static void a () {
System.out.println ( “static a() “);
}

Public void b () {
System.out.println ( “b() “);
}

Public static void c () {
System.out.println ( “x is “+x); // static variable a can be accessed
a() ; // static method a() can be accessed
/*
y = 34; // non static variable y cannot be accessed in static method
b(); // non static method b() cannot be accessed in static method
*/
}
}

public class Test{

Public static void main(String[] args) {
Number.c(); // accessing static method using class name
Number n=new Number();
n.c(); // accessing static method using object
}
}

Output:
x is 7
static a()
x is 7
static a()
Invalid code in Number class has been commented out using a multi-line comment. The code contained in that Comment is invalid since an attempt was made to access non static methods and variables from a static method. The Test class shows how static methods can be accessed using both the class name and method name.
Author: , 0000-00-00