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 |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
(the same thing as
)
|
|
|
and any classes
|
|
Examples:
Code | Note |
---|---|
|
|
|
|
|
|
|
|
|
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 |
---|---|---|
|
|
|
|
|
|
|
|
The variable is private . It is not visible outside the class. |
|
|
The variable is private . It is not visible outside the class. |
|
|
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 |
---|
|
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 |
---|
|
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:
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:
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:
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:
GO TO FULL VERSION