Class members
An object of a class may have fields and methods. Objects differs in values of the fields. A class may also have fields and methods which are common for all objects. Such fields and methods are known as static members. To declare them you should write the keyword static
.
Class variables
A class variable (static field) is a field declared with the keyword static
. It can have any primitive or reference type like a regular instance field. A static field has the same value for all instances of the class. It belongs to the class, rather than to an instance of the class.
Sometimes we'd like to have a common value for all instances of a class like a global variable. Then it's much better to declare it static as this can save memory because a single copy of a static variable is shared by created objects.
Static variables can be accessed directly by the class name. To access a static field you should write:
ClassName.fieldName;
Example 1. Here is a class with two public static variables:
class SomeClass {
public static String staticStringField;
public static int staticIntField;
}
We can set and get them:
SomeClass.staticIntField = 10;
SomeClass.staticStringField = "it's is a static member";
System.out.println(SomeClass.staticIntField); // It prints "10"
System.out.println(SomeClass.staticStringField); // It prints "it's is a static member"
Note, in general case, it's not a good idea to declare public static fields, it's just an example.
Also, it's possible to access the value of a static field through an instance of the class.
SomeClass.staticIntField = 30;
SomeClass instance = new SomeClass();
System.out.println(instance.staticIntField); // It prints "30"
Example 2. Let's see a more interesting example. Here is a class with the static field lastCreated
. The field stores the date of the last created instance.
public class SomeClass {
public static Date lastCreated;
public SomeClass() {
lastCreated = new Date();
}
}
The static field is changed in the class constructor every time when a new object is created.
The code below creates two instances and outputs intermediate results:
System.out.println(SomeClass.lastCreated);
SomeClass instance1 = new SomeClass();
System.out.println(SomeClass.lastCreated));
SomeClass instance2 = new SomeClass();
System.out.println(SomeClass.lastCreated));
In my case, the results were:
null
Sun Aug 20 17:49:24 YEKT 2017
Sun Aug 20 17:49:25 YEKT 2017
Class constants
Static fields with the keyword final
are class constants. They can not be changed. According to the naming convention, static fields always should be written in the upper case, use the underscore (_) to separate parts of the name.
The standard class Math
contains two static constants:
public static final double E = 2.7182818284590452354;
public static final double PI = 3.14159265358979323846;
Constants are often public, but it's not a rule.
Example. Let's declare a class named Physics
with two static constants:
class Physics {
/**
* The speed of light in a vacuum (m/s)
*/
public static final long SPEED_OF_LIGHT = 299_792_458;
/**
* Electron mass (kg)
*/
public static final double ELECTRON_MASS = 9.10938356e-31;
}
To use the constants let's write something like the following code:
System.out.println(Physics.ELECTRON_MASS); // 9.10938356E-31
System.out.println(Physics.SPEED_OF_LIGHT); // 299792458
We cannot change the value of a constant:
Physics.ELECTRON_MASS = 10; // compile-time error
Class methods
A class may have static methods as well as static fields. Such methods also are known as class methods. A static method can be accessed by the class name and doesn't need an object.
Static methods can be called directly by the class name. To access a method you should write:
ClassName.staticMethodName(args);
A static method can have (have no) arguments like a regular instance method.
But, unlike instance methods, static methods have several features:
- a static method can access only static fields, it cannot access non-static fields;
- a static method can invoke another static method, but it cannot invoke instance method;
- a static method cannot refer to
this
keyword because there is no an instance in the static context.
Instance methods can access static fields and methods.
The Java class library provides a lot of static methods in different classes:
- the class
Math
has a lot of static methods, such asMath.min(a, b)
,Math.abs(val)
,Math.pow(x, y)
and so on; - the class
Arrays
has a lot of static methods for processing arrays; Long.valueOf(...)
,Integer.parseInt(...)
,String.valueOf(...)
are static methods too.
Example. Here is a class with one constructor, a static and an instance method.
public class SomeClass {
public SomeClass() {
invokeAnInstanceMethod(); // it's possible here
invokeAStaticMethod(); // it's possible here too
}
public static void invokeAStaticMethod() {
// it's impossible to invoke invokeAnInstanceMethod() here
}
public void invokeAnInstanceMethod() {
invokeAStaticMethod(); // it's possible too
}
}
This example shows, you can invoke a static method from the instance context (constructors and instance methods), but you can't invoke an instance method from a static context.
An example of a static method is the main
method. It always should be static.