Lesson: Static Modifier

Explore the concept of static fields and methods.

Understand what static fields and methods are and how and when to use them.


Normally, class members (variables and methods) are accessed via an instance reference. Leaving methods aside for the moment, this is because class variables exist separately for each instance of a class (created with the new keyword). If you have a variable x in a class and create two instances of the class, each instance will have its own x variable, access to which is by the instance reference. You also access methods via the instance reference. Here is an example:

The result:

theVar=3   theVar=7

Instance1 and instance2 refer to separate object instances of the class and as such each has its own theVar variable which has its own value. The variable is said to be an instance variable.

What if we would like to have a class level or global variable? One that is not specific to any instance of the class but exists as a single copy in memory at  the class level? We can do that with the static modifier. Marking a variable as static means there is only one copy of the variable for all class instances. The static variable is created when first accessed and persists as long as the program runs. Any instance of the class can access the static variable as it is shared among all instances of the class.

Since static variables are not accessed via an instance reference, you use the class name with a dot to access the variable.

You can also mark methods as static. This means the method does not need an instance reference to be called. The method is class level or global. Note that static methods can only access the static variables in the same class. Non-static or instance methods can access static and instance variables. Here is an example:

This example would print out:

instance count=0
instance count=3

The example uses a static variable to count how many instances of MyClass are created. We increment globalCount in the class constructor. This would make globalCount = 2 but to demonstrate static variable access, we directly increment globalCount to 3. We can do this since globalCount has public access. Note that the first output is zero because we called the static method which caused the static globalCount varible to be created and initialized, but we have not yet created any instances of the class. Note that the last statement would generate a compile error since we are accessing a non-static variable through a static (class name) reference.

Here is the example above on CodingGround. Fix the error and demonstrate the program.

Here are two videos (video1, video2) about static members. Here is a detailed discussion of static members.

Note: While classes can't normally be labelled static, an inner class, that is a class within a class, can be. We won't dicuss this as it is beyond the scope of this curriculum, but the use of the static keyword on the inner classes in our CodingGround examples is required when inner classes are defined in the same class as the main() method.