What is a static variable in Java

Ioannis Anifantakis
3 min readApr 5, 2019

A static variable is a variable that doesn’t belong to an instance of that class, but to the class itself.

Equivalently a static method belongs to the class, rather than an object instance.

Let’s take the example of a very simple class:

public class Person(){
private static int population = 0;
private String name;
private String surname;

public Person(String name, String surname){
population++;
this.name=name;
this.surname=surname;
}

/**
* A member variable that returns the specific
* object's full name.
*/

public String getFullName(){
return name+" "+surname;
}

/**
* A static method that returns the total
* amount of person objects created.
*/

public static int getPopulation(){
return population;
}
}

Ok, above you have 3 variables. One static variable and 2 member variables. Each object will have its own name and surname, so the member variables will be different for every object.

However, the static variable “population”, will belong to the class Person, and not to a particular instance of this class.

// Static variable
private static int population = 0;
// member variables
private String name;
private String surname;

In our constructor method, we initialize the member variables (name and surname) of the newly created object and then, we increment the total amount of persons by 1 via the static population variable.

public Person(String name, String surname){
population++;
this.name=name;
this.surname=surname;
}

Even if there are no objects created, we can still ask for the population of the Persons class without creating anything because “population” is a static variable (it will return 0 population if we haven’t created any object).

// create a STATIC method that returns the STATIC population value
public static int getPopulation(){
return population;
}
// by calling this STATIC METHOD
// we get the population of the Person class

print(Person.getPopulation())
// it is the same as calling the STATIC VARIABLE
// we get the population of the Person class
print(Person.population);

We can simply call Person.getPopulation() to retrieve the current population of the Person class…

Then if we create a new object, let’s say,
Person p1 = new Person(“Ioannis”, “Anifantakis”);
then the population that belongs to the Person class increases to 1.

if we create yet another person, let’s say Person
p2 = new Person(“Eleni”, “Fragioudaki”);
then again we query for the population of the class of persons, rather than some property of p1 or p2 object.

p1 = new Person("Ioannis", "Anifantakis");
print(p1.getFullName()) // print result: Ioannis Anifantakis
print(Person.getPopulation()); // print result: 1
p2 = new Person("Eleni", "Fragioudaki");
print(p2.getFullName()) // print result: Eleni Fragioudaki
print(Person.getPopulation()); // print result: 2

/*
you observe that the member method "getFullName()" which is called in object level, returns the values of name and surname for that specific instance (object) of Person.
While the static method getPopulation() which is called in class level, returns a value that is shared among all objects, and its not object specific (does not belong to a specific person instance, but to all persons).
*/

So since the population refers to all the persons, then we access it directly via a class call (aka Person.population for the variable, and Person.getPopulation() for the method).

Thus, static relates more to procedural programming, that is it creates variables and methods that relate to the total of the class, not to a specific instance (aka a specific object).

// to print the full name of a specified object
// we use the object itself as reference
// output -> "Ioannis Anifantakis"
print(p1.getFullName());
// to print the population that belongs to the entire class
// we use the class itself as a reference
// output -> 2
print(Person.population);

Static Variables and Methods vs Member Variables and Methods

Member variables are unique for every created object instance. That is every person will have a different name and surname.

However, if we increase the population when a person is created, then the static variable “population” will increase. And since this variable belongs to the total of the persons, it will not be accessed via a specific object, but via the class itself.

So this is why we refer to “Person.population” to access the private population variable since it is a variable that belongs to the Person class as a total.

TL;DR

Static variables and methods belong to the class and they represent a common property that is the same for all the objects, rather than a property that can differ per object.

--

--

Ioannis Anifantakis

MSc Computer Science. — Software engineer and programming instructor. Actively involved in Android Development and Deep Learning.