1. Static variables

When a class is loaded into memory a static object is created immediately. This object stores static class variables (static class fields). The static object exists even if no ordinary (non-static) objects of the class have been created.

When we declare variables in a class, we indicate whether they will be created just once, or whether there should be distinct instances of these variables in each object. By default, a new copy of each variable is created for each object.

A static variable is bound to the class's static object and there is always a single instance of it.

To create a static variable in a class, you need to write the static keyword before its name. The general format for declaring a static variable is:

static Type name = value;

If a static variable is not assigned an initial value, it is initialized with a default value:

Type Default value
byte
0
short
0
int
0
long
0
float
0.0
double
0.0
char
'\u0000'
(the same thing as
0
)
boolean
false
Object
and any classes
null

Examples:

Code Note
public static int WIDTH = 100;
public static String message = "Hello";
private static Scanner console;
private static int count = 0;
private static boolean flag;

Inside of a class, you can refer to its static variables simply by using their names. But to access them from another class, you need to write the name of the class before the name of the static variable.

ClassName.variable

Example:

Variable Class Accessing a variable outside its class
public static int WIDTH = 100;
Solution
Solution.WIDTH
public static String message = "Hello";
Main
Main.message
private static Scanner console;
JavaGame
The variable is private. It is not visible outside the class.
private static int count = 0;
Counter
The variable is private. It is not visible outside the class.
private static boolean flag;
Arithmetic
The variable is private. It is not visible outside the class.


2. Difference between static and non-static variables

Non-static (ordinary) variables of a class are declared in the same way as static ones, only without the static keyword.

What's the difference between ordinary variables and static variables?

Ordinary variables of a class are bound to objects of the class (instances of the class), while static variables are bound to the class's static object.

If there are multiple instances of a class, each of them has its own copy of the non-static (ordinary) variables. Static variables of a class are always stored in its static object and only a single instance of them exists.

You can access the ordinary variables (fields) of a class only if you have a reference to an object of the class. And inside methods of the class, of course.

Example:

Accessing a field of a class using an object reference
public class DataInfo
{
   public int value = 100;
}

public class Solution
{
   public static void main(String[] args)
   {
      DataInfo info = new DataInfo();
      System.out.println(info.value);
   }
}

You can access static variables from anywhere (after accounting for the visibility modifiers): from ordinary methods, from static methods of the same class, from methods of other classes, etc.

Example:

Accessing a static field of a class without using an object reference
public class DataInfo
{
   public static int value = 100;
}

public class Solution
{
   public static void main(String[] args)
   {
      System.out.println(DataInfo.value);
   }
}

How memory is organized:

Let's say we have a Person class with 4 fields: two are static and two are not.

public class Person
{
   public static int count = 0;
   public static int sum = 0;
   public int age = 0;
   public String name;
}

Immediately after loading the class

When the Java machine has finished loading the Person class, memory will look like this:

After creating the first object

If we create a Person object, then the picture becomes this:

Difference between static and non-static variables 2

Please note that although both objects have two variables, these are different variables: the ordinary objects have ordinary variables, and the static object has the static variables.

We need more objects

Let's create two more Person objects. Now memory will look like this:

Difference between static and non-static variables 3

Please note that each object has its own age and name variables.



3. Removing and adding the static modifier

From static to ordinary

What happens if we take a static variable and turn it into an ordinary one by removing its static modifier? For example, the static int sum variable.

The modified code will look like this:

public class Person
{
   public static int count = 0;
   public int sum = 0;
   public int age = 0;
   public String name;
}

Now memory looks like this:

Difference between static and non-static variables

The static variable disappeared from the static object, and now each ordinary object has its own sum variable.

From ordinary to static

We can do the opposite: add the static modifier to ordinary variables of a class. They will disappear from all ordinary objects and will appear in the static object. Suppose we decide to make the age and name variables static. Then the code will look like this:

public class Person
{
   public static int count = 0;
   public int sum = 0;
   public static int age = 0;
   public static String name;
}

And now memory will look like this:

Removing and adding the static modifier