Static Keyword in Java

We often see public static void main (String[] args){} in all the standalone java applications. The part of the statement we are particularly interested in, as of now, is Static.

Like the Final keyword, static keyword can also be used with multiple entities in Java.

  1. Nested Classes
  2. Methods
  3. Variables
  4. Initialization Blocks

Static keyword cannot be used with constructors, top level classes, method local inner classes, inner class methods and instance variables and local variables.

The basic idea behind the static keyword is to make the entity available at Class level not the instance level.

Whenever we define any non-static variable and method it actually gets associated with the Object (Instance of the class), not with the Class.

I know this might be confusing a little bit, but hold on we will discuss for all the entities in detail.

Basically, static modifier is used to create entities that will exist independently of the any instance of the class. All the static member will exist even before the construction of the instance, which means even if you don’t instantiate the object (instance of a class), you still can use the static variables.

So, there will be only one copy of the static members, no matter how many instances we create for that class. That’s why whenever any instance try to access the value of any static variables they all will get the same value, because they share a common reference to the static variable.

Now let’s start with Variables and Methods.

This is a simple class in Java, where we have some variables and some methods (getters and setters in our case).

The typical way to access these variables and methods are –

Here if you observe that, to access and use the variables and methods of Student class, you need at least one object. Only through an object reference, you can access these variables and methods. An object in Java are called an instance of a class. That’s why these variables are called instance variables. These variables are associated with instance, which means that each instance will have their own copy of variables, and their values are not shared among the other instances.

Now, suppose we need to maintain a count of student objects. Every time student object is created the count should increase by one. But according to the above snippet and our discussion until now, if we use any instance variable like studentCount, then it will be initialize every time the object created, because every instance will have their own copy. So, we need some mechanism through which we can maintain this count at a Class level, such that every time the object is created, the same count variable is updated (unlike the different Student instance variables getting initialized every time at Student object creation) and we can get the total count of student instances created from literally anywhere in our application.

What I said will be more clear to you with the below snippet –

So, static is a keyword, when applied to any variable, the compiler treats it as a class resource and not an instance resource. In other words, the compiler keeps only one copy of the static variable at the class level, and the same copy is available to all of the class instances.

So, output of the program would exactly be as you guessed –

Here, did you noticed one thing?

Yes, you might have noticed how we have accessed the studentCount variable in the sysout line.

We have accessed the studentCount variable directly through the class name and we did not need any instance reference to do so. This proves that studentCount is actually a class level resource and should be accessed using the class name. That is the reason, why we have specifically mentioned that, no matter whether any instance is created or not, we still can access a static resource.

The same thing is applicable to the static methods, where a static method can be accessed directly with the class name.

Static methods are mainly used in Utility Classes to create Utility methods, where the method behavior is not dependent on the value/state of any instance. Utility methods are generally the methods which accepts some input, processes them and gives back the output, without any dependency on the instance variable of the class in which the method has been put.

Let’s finally understand some hidden characteristics of static keyword

  1. A static method cannot use any instance variable and cannot call to any non-static method DIRECTLY – Its very obvious, isn’t it? How would a static method know which instance variable to use, or the which instance method to call. If there are multiple objects of the same class, there will be that many copies of the instance variable, so compiler will get confused which one to use, as static method will be only one at a class level. So, whenever we try to use non-static member in static method, compiler gives an error. However, we can use the non-static members of a particular class by instantiating in the static method.
  2. Instance methods can use static members – This makes sense. After all, there is only one copy of static resource at the class level, so the method of all instances will be using the same resource, so there is no ambiguity.

Static Blocks

We also have something called as the static blocks, which we can use to initialize any static member of the class.

Static blocks get executed at the time of class loading.

Let’s try to understand the below snippet –

Running the above class will produce the following output:

Now let’s go back to the class we created earlier in this article to count the number of Student instances. Note that, there we had initialized the static variable studentCount at the time of its declaration, something like:

But the above statement can be used alternatively with a static block as:

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.