In this post, we will compare and contrast static and non-static variables in Java, and explain their advantages and disadvantages.

1. Overview of a static variable

A static variable is a variable that is declared with the static keyword in a class. A static variable belongs to the class itself rather than to any instance of the class. A static variable has only one copy that is shared by all objects of the class. A static variable can be accessed directly using the class name followed by the variable name (e.g., ClassName.variableName). It has the following characteristics:

  • A static variable allows fast access to data by using the class name rather than an object reference. It does not require creating an object to access or modify the data.
  • A static variable saves memory by having only one copy that is shared by all objects of the class. It can be memory-efficient when the same data must be shared across all objects.
  • A static variable can be used to store constants or global variables that are not specific to any instance of the class. For example, a Math class containing mathematical constants or functions.
  • A static variable can be used to implement the singleton design pattern, which ensures that only one instance of a class is created and provides a global access point to it.

2. Overview of a non-static variable

A non-static variable is a variable that is declared without the static keyword in a class. A non-static variable belongs to each instance of the class rather than to the class itself. A non-static variable has multiple copies that are unique to each object of the class. A non-static variable can be accessed using an object reference followed by the variable name (e.g., objectReference.variableName). It has the following characteristics:

  • A non-static variable allows instance-specific behavior by having different values for different objects of the class. It can be used to store data that is specific to each object.
  • A non-static variable supports encapsulation and data hiding by using access modifiers such as private, protected, or public. It can be used to restrict or allow access to the data from other classes or objects.
  • A non-static variable supports inheritance and polymorphism by allowing subclasses to inherit or override the data from superclasses. It can be used to implement the concept of IS-A relationship between classes.
  • A non-static variable supports abstraction and interface by allowing classes to implement the data from abstract classes or interfaces. It can be used to implement the concept of HAS-A relationship between classes.

3. Example of static and non-static variables

Consider the following program, which has a counter static variable to store the total number of instances of the Util class created so far. To demonstrate the difference between static and non-static member variables, remove the static keyword from the counter variable, and note the difference in the output:

Download  Run Code

Output:

Total instances so far: 1
Total instances so far: 2
Total instances so far: 3

4. Difference between static and non-static variables

The functionality of static and non-static variables are similar. However, they also have some differences and trade-offs that we must understand:

  1. Like static methods, a static variable belongs to the class itself, and a non-static variable belongs to each instance of a class. Therefore, the value of a static variable remains the same for each instance of the class, but the same cannot be said for the non-static variable.
  2. We cannot access non-static variables inside a static method without an instance of its class. A static method can only access static variables while a non-static method can access both static and non-static variables.
  3. Static variables reduce the memory footprint of the program. This is because the memory is allocated only once for a static variable during the time of class loading, while for a non-static variable, memory is allocated every time an instance of the class is created.
  4. Static variables are usually declared as final in Java. This ensures the value never gets changed after its initialization. This is very useful when we need a single copy of the variable to be shared across all class instances.
  5. Static variables can be accessed using the class name, while a reference to a non-static variable needs an instance of the class. For example, the standard output stream out is declared static in the System class, called using System.out.

5. What to use and when?

As we have seen, static and non-static variables are both useful types of variables to store and process data in Java. Here are some general recommendations on how to choose between them:

  • If you need a variable to store a piece of data that is common to all objects of a class and does not change over time, we can use a static variable.
  • If you need a variable to store a piece of data that is specific to each object of a class and may change over time, we should use a non-static variable.

That’s all about static and non-static variables in Java.

 
Also See:

Static and non-static methods in Java