Java
October 19, 2020

Theory: Static members

All objects of a class have the same fields and methods, but the values of the fields of objects are usually different. At the same time, 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.

In this topic, you will learn how to use static class members. As a bonus, you will finally understand what does the static keyword mean in the declaration of the main method.

Class variables

A class variable (static field) is a field declared with the keyword static. It can have any primitive or reference type, just 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.

If we want all instances of a class to share a common value, for example, a global variable, then it's better to declare it as static. This can save memory because a single copy of a static variable is shared by all created objects.

Static variables can be accessed directly by the class name. To access a static field you should write:

ClassName.fieldName;

Class constants

Static fields with the keyword final are class constants. They can not be changed. According to the naming convention, constant fields should always be written in the upper case with the underscore (_) to separate parts of the name.

Constants are often public, but it's not a rule.

Class methods

A class may have static methods as well as static fields. Such methods are also known as class methods. A static method can be accessed by the class name and doesn't need an object of the class.

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 arguments like a regular instance method or have no arguments. But, unlike instance methods, static methods have several special 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 an instance method;
  • a static method cannot refer to this keyword because there is no instance in the static context.

Instance methods, however, can access static fields and methods.

Static methods are often used as utility methods that are common for the whole project. As an example, you can create a class with only static methods for performing typical math operations.

Example. Here is a class with one constructor, a static and an instance method.

This example shows that 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.

The only way to call an instance method from a static one is to provide a reference to this instance as an argument. You can also create objects of other classes and call their methods in a similar way. Here's an example:

An example of a static method is the main method. It always should be static.