Sitemap
Manisha Shukla

Manisha Shukla

I write on software testing( Manual testing ,Automation,API, Java etc.)

Static Keyword in Java

2 min readMay 20, 2025

In Java, the static keyword is used to declare members (variables, methods, and nested classes) that belong to the class rather than instances of the class. When a member is declared static, it can be accessed without any object and without reference to an object.

Here are the main uses of the static keyword in Java:

Static Variables (Class Variables): When a variable is declared with the static keyword, it becomes a class variable. It is shared among all instances of the class. There is only one copy of the static variable, and it is created when the class is loaded into memory.

For non-static members, it depends on object creation. If you create object ’n’ a number of times then the copy will create ’n’ a number of times.

public class MyClass {

static int staticVariable;

// Rest of the class code…

}

Accessing a static variable is done using the class name, not through an instance of the class.

Example:

MyClass.staticVariable = 10;

Static Methods: Similar to static variables, static methods belong to the class rather than an instance. They can be called using the class name without creating an instance of the class.

Example:

public class MyClass {

static void myStaticMethod() {

// Code for the static method…

}

// Rest of the class code…

}

Calling a static method:

MyClass.myStaticMethod();

Static Blocks: A static block is a block of code enclosed in curly braces and preceded by the static keyword. It is executed when the class is loaded into the memory. This is often used for static initialization.

Static blocks are executed only once when the class is loaded.

Example:

public class MyClass {

static {

// Code in the static block…

}

// Rest of the class code…

}

Get Manisha Shukla’s stories in your inbox

Join Medium for free to get updates from this writer.

https://medium.com/@manisha10850/static-initialization-block-in-java-ef007a4acc18

https://quickqatopics.blogspot.com/2023/11/static-initialization-block-in-java.html

Example:

class Test {

static int a = m1();

static{

System.out.println(“static block!”);

}

static int m1(){

System.out.println(“from m1!”);

return 20;

}

public static void main(String[] args) {

System.out.println(“Value of a!” + a);

System.out.println(“from main”);

}

}

Output:

from m1!

static block!

Value of a!20

from main

Static Nested Classes: If a class is defined within another class as a static member, it is called a static nested class. It can be instantiated without creating an instance of the outer class.

Example:

public class OuterClass {

static class StaticNestedClass {

// Code for the static nested class…

}

}

Creating an instance of the static nested class:

OuterClass.StaticNestedClass nestedObj = new OuterClass.StaticNestedClass();

Using static appropriately can lead to more efficient memory usage and can be beneficial when certain members of a class are intended to be shared among all instances of that class.

--

--

Manisha Shukla
Manisha Shukla

Written by Manisha Shukla

I write on software testing( Manual testing ,Automation,API, Java etc.)