Java: static
static
keyword can be used in the declaration of variables and methods.- A variable declared with
static
is called “class variable”. - A method declared with
static
is called “class method”. - Variables/methods without static keyword are called “instance variable” and “instance method”.
- the
static
keyword cannot be used in class declaration.
Purpose of class variable and methods are:
- class variable has only one copy (1 value). All objects (instances)'s value of the static variable will be the same. Instance variable has a copy/value for each object.
- class method do not need to be instantiated before call. They are called directly, for example:
MyClass.f(4)
Following are details.
Class Variable vs Instance Variable
“static” Keyword = Class Variable
Variables can be declared with the static
keyword.
static int y = 0;
When a variable is declared with the keyword static
, its called a class variable.
- For a class variable, all instances of the class share the same copy of the variable.
- A class variable can be accessed directly with the class, without the need to create a instance. By the syntax
class_name.static_var_name
.
No “static” Keyword = Instance Variable
Without the static
keyword, it's called instance variable, and each instance of the class has its own copy of the variable.
// example of instance vs class variable class T2 { int x = 0; // instance variable static int y = 0; // class variable void setX (int n) { x = n;} void setY (int n) { y = n;} int getX () { return x;} int getY () { return y;} } class T1 { public static void main(String[] arg) { T2 b1 = new T2(); T2 b2 = new T2(); b1.setX(9); b2.setX(10); // each b1 and b2 has separate copies of x System.out.println( b1.getX() ); System.out.println( b2.getX() ); // class variable can be used directly without a instance of it. //(if changed to T2.x, it won't compile) System.out.println( T2.y ); T2.y = 7; System.out.println( T2.y ); // class variable can be manipulated thru methods as usual b1.setY(T2.y+1); System.out.println( b1.getY() ); } }
Instance Method vs Class Methods
Methods can also be declared with the keyword static
.
When a method is declared static, it can be used without having to create a object first.
For example, you can define a collection of math functions in a class, all static, using them like functions.
Methods declared with “static” keyword are called class methods. Otherwise they are instance methods.
// A class with a static method class M2 { static int triple (int n) {return 3*n;}} class M1 { public static void main(String[] arg) { // calling static methods without creating a instance System.out.println( M2.triple(4) ); // calling static methods thru a instance is also allowed M2 m1 = new M2(); System.out.println( m1.triple(5) ); } }
Static Methods Cannot Access Non-Static Variables
Methods declared with static
cannot access variables declared without static
.
The following gives a compilation error, unless x
is also static.
class V2 { int x = 3; static int returnIt () { return x;} } class V1 { public static void main(String[] arg) { System.out.println( V2.returnIt() ); } }
~ $ javac V1.java V1.java:3: error: non-static variable x cannot be referenced from a static context static int returnIt () { return x;} ^ 1 error ~ $