Java Static Keyword

Java Static keyword is a non-access modifier. We can use static keyword with variable, method, block and class. Class can directly access static variables and methods.

Static Block

Most of the tutorials don’t discuss about static block in detail which is very important to understand.

Java static keyword is used to initialize the data variables and is always called before the main method execution either it is declare before or after main method.

In the below example, we have defined two static blocks with one static variable, one before the main method and another one after the methods. static blocks get execute in same sequence they define(from top to bottom).

In the output, we can clearly see the static blocks and variable executed before the main method execution. This static block, variable, method, and class initialize once the class gets loaded.


Static Variables

Static variables get assign memory in the class area as soon as class loads and It will be there in memory until the program execution is complete and are common to all the class object. This means if any object is changing static variable value, this will get change for all other rest of the objects.

static variables can be accessed in both static and non static methods.

Below is the syntax to define static variable:

static <data_type> <variable_name> = <variable_value>;


As we see in the below examples, we can access a static variable alone, using an instance, and using a class name.

The class level static variable is declared with in a value will always remain the same for all objects.

As mentioned above and relate the same with below code snippet, the class level static i variable is declared with a value of 1, and it will always remain the same for all objects. On line two, we declared a static variable i with the value of 1 and printed it on lines 10, 11, and 12. It should be noted that at line 16, we used object obj to update the i value to 2. If we print the i value using obj, obj2 or class name, it will print the same value for all as 2.


Static Methods

A method that belongs to a class and declare as static is called a static method. A static method can also be accessed using a class name and object name.

The main purpose of the static method is to access it independently by using the class name in place of creating an instance and then accessing it. This means we can execute static functions in case object creation is also not done.

Note: We can access static function using an object or instance.

<access_modifier> static <return_type> <function_name> () { }

e.g. public static void printNumber() { }


Below is the best example of print() method defined on line 3 and accessing it on lines 10 and 13 without an object or instance.

Import Points:

  1. Static methods can not be overridden as they are class dependent.
  2. A static method can be called without an object or instance.
  3. Static methods will always share the same definition with all instances.
  4. Mostly utility classes used to have static methods to define common functionality which can be consumed by multiple classes.
  5. Static methods will always access static data member.
  6. this and super not allowed within static method and block.

Most asked interview question:

why main method is not static ?

It’s because the mail method can be called without class name by JVM.

Static Class

We can not define outer class as static. Only inner class which is class inside class can be define as static.

Syntax Example:

Below is the example of static nested class.


As Inner class is static and can be access with class name as “Outer.Inner” to create its instance using new keyword.

Inner Class !!!!

Best example of static nested class is to create singleton object

Singleton object: 

Class that will have single object throughout application called as singleton object.

The creation of a singleton class is an interesting topic and asked in many interviews. Let’s discuss the same in detail.

  1. Line number 1, Make the SingletonClass constructor private so that no one can create its object form outside of the current class.
  2. Line number 4, create a static inner class “SingletonInnerClass”.
  3. Line number 5, create a static final instance of SingletonClass. Create a static -> Anywhere get access using class name.
    final -> To get initialized once.
  4. Create a static method which will return “SingletonClass” instance within “SingletonClass” to access static instance variables create as part of point number 3.
  5. SingletonClass.getInstance() will always return same instance of “SingletonClass”.

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan