Java Static Keyword: The Complete Guide

Static Keyword in Java Tutorial | Java Static Keyword Example

The main method of a class in Java is generally labeled static. Java Static keyword is used for a constant variable or a method that is the same for every class instance. Let’s deep dive into static keyword and see some examples.

Java Static Keyword

The static keyword in Java is used mainly for memory management. It is used with variables, methods, blocks, and nested classes. It is a keyword used to share the same variable or method of a given class.

While using a static variable or method, there is no need to create an object. Instead, use the class name before the static variable, method, block, or nested class.

Only one instance of the static member is created, which is shared across all the class instances. A static method cannot call the non-static method; it can only call the static method.

Static is the non-access modifier in Java which is applicable for the following:

  1. blocks
  2. variables
  3. methods
  4. nested classes

Static Keyword in Java Tutorial

Now we will discuss all types of methods which we can use one by one.

Static Variable

  1. It is also known as a class variable.
  2. The static variable allocates memory only once in the class area during class loading, i.e., it saves memory.
  3. The static variable is used in real scenarios.
  4. Static variables can only be declared at a class level.
  5. Static fields can be accessed without the object initialization.

In Java, if the field is declared static, precisely the single copy of that field is created and shared among all the instances of that class.

It doesn’t matter how many times we initialize the class; there will always be only one copy of the static field. The value of that static field will be shared across all the objects of either the same or any different class.

From a memory perspective, static variables go in the particular pool in JVM memory called Metaspace (before Java 8, this pool was called a Permanent Generation or PermGen, which was removed entirely and replaced with a Metaspace).

Compelling Reasons to Use static Fields

  1. When the value of the variable is independent of objects.
  2. When a value is supposed to be shared across all the objects.

For Example, the company name of employees, school names of students, etc. The name of the school is common for all students in that school.

Syntax to proclaim static variable

public static name;

Let us understand it using the code given below.

class Variable 
	public static String name="Static Variable";
	public static void main(String [] args)


static variable in java

In the above code, we know how the static variable is initialized (using static keyword) and accessed(using the class name before the static variable, e.g.,

Static Method in Java

  1. It is also known as the class method.
  2. The static method belongs to a class rather than the object of a class.
  3. Memory is allocated only once during class loading, and a static method can be executed only once in the program.
  4. Instance methods can directly access both the instance methods and instance variables
  5. Instance methods can also access the static variables and static methods directly
  6. static methods can access all the static variables and other static methods
  7. The following combinations of an instance, class methods, and variables are valid:
    1. Instance methods can directly access both instance methods and instance variables
    2. Instance methods can also access static variables and static methods directly
    3. Static methods can access all static variables and other static methods
    4. Static methods cannot access instance variables and instance methods directly; they need some object reference.

When a method is declared with the static keyword, it is known as a static method. The most common example of the static method is the main( ) method.

They can only directly call the other static methods. They can only directly access the static data. They cannot refer to this or the super keyword in any way.

For example, the Factory design pattern (instance of a class can be created only once) provides good use of the static method. 

You can use the static method to create an instance of a class.

Syntax to proclaim static methods

static void method1
	//relevant Codes

Let us understand it using the code given below.

class Main
	//static method declaring 
	static void m1()
		System.out.println("I am Static Method");
//driver class 
class Method
	public static void main(String args[])
		Main.m1();  // static method calling 


Static methods in Java

In the above code, we know how a static method is declared (static void m1() {}) and executed (using the class name, e.g., Main.m1()).

Static Block

  1. It is used to initialize a static variable.
  2. A class can have multiple static blocks.
  3. It is a set of statements, and JVM executes it before the execution of the main method (i.e., public static void main (String [] args)).

For example, When we want a statement to be executed before the main method. Static methods are used, and it is the same as the static variable for initialization of static variable or sometimes includes a set of statements to be executed before the main method.

Syntax to proclaim static block

    //Set of statements 

Let us understand it using the code given below.

//creating a Block class 
class Block
	//decalring static variables
	static int a=10;
	static int b;
	//decalring static block 
		System.out.println("i am Static block");
	//main method 
	public static void main(String args[])

See the following output.

Static block in Java

We understand how a static block is declared and executed in the above code without explicitly calling it. And how the logic is performed and then run before executing the main method.

Static Nested Class

  1. Nested classes that are declared as static is called static nested class.
  2. The static nested classes only have access to static members of the outer class, including the private ones.
  3. A static nested class is used in scenarios.

For example, when we do not want to create a mess of creating the instance of the outer class, it is similar to the inner class. Still, the difference is that while creating the object or instance of the nested static inner class, you don’t need an instance of the outer class.

Syntax to proclaim static nested class

class A
    static class B


Let us understand it using the code given below.

class Outer1
   static int a=50;

   //static inner class declaring 
   static class Inner1
      void f()
   public static void main(String args[])
	 Outer1.Inner1 obj=new Outer1.Inner1();  

See the following output.

static nested class

In the above code, we understand how the nested inner class is defined and executed by creating the class instance (e.g., Outer1.Inner1 obj = new Outer1.Inner1()).

When to use static variables and methods

Use a static variable for the property common to all the objects. For example, students share the same college name in the class Student. Use static methods for changing static variables.


  1. Static is a keyword in Java.
  2. The Static method can be called using the class name no need to create the class object.
  3. Static block is executed before a main method of the program
  4. Static variables and blocks are executed in the way they are present in the program.
  5. A class can have multiple static blocks.
  6. Memory is allocated only once at a time of loading.
  7. The static nested class cannot access non-static (instance) data members or methods.

In short, the Static keyword can be used with class, variable, method, and block. Static members belong to the class instead of a specific instance, and this means if you make a member static, you can access it without an object. 

That’s it for this tutorial.

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.