Static Keyword in Java

2
1

Introduction

The static keyword in Java is a non-access modifier used to create members that belong to a class instead of individual objects.

A static member is shared by all objects of the class and can be accessed using the class name. An object is not required to access it.

The static keyword is commonly used with:

  • Variables

  • Methods

  • Blocks

Static Keyword

Static Keyword

Static Variable in Java

A static variable, also called a class variable, belongs to the class and is shared among all its objects.

Only one copy of a static variable is created. If one object changes its value, the updated value is visible to the other objects.

Static Variable Example

// Class used to count created objects
class Counter {
// Static variable is shared by all Counter objects
static int count = 0;
// Constructor runs whenever an object is created
Counter() {
// Increase the shared static variable
count++;
}
}
public class Main {
public static void main(String[] args) {
// Create the first Counter object
Counter first = new Counter();
// Create the second Counter object
Counter second = new Counter();
// Access the static variable using the class name
System.out.println("Total objects: " + Counter.count); // Output :- Total objects: 2
}
}

In this example, count is shared by both objects. Each time an object is created, the same variable is increased.

Static Variable

Static Variable

Static Method in Java

A static method belongs to the class instead of an object. It can be called directly using the class name.

Static methods are commonly used for operations that do not depend on an object’s individual data.

Static Method Example

// Utility class containing a static method
class MathUtils {
// Static method calculates the square of a number
static int square(int number) {
// Return the calculated result
return number * number;
}
}
public class Main {
public static void main(String[] args) {
// Call the static method using the class name
int result = MathUtils.square(5);
// Display the returned value
System.out.println(result); // Output :- 25
}
}

The square() method is called using MathUtils.square(5). No MathUtils object is required.

Static Method

Static Method

Static Block in Java

A static block is a block of code used to initialize static variables. It runs once when the class is initialized.

A static block runs before objects of the class are created and before the class’s static members are used.

Static Block Example

// Class containing a static variable and static block
class Configuration {
// Declare a static variable
static String applicationName;
// Static block runs once when the class is initialized
static {
// Initialize the static variable
applicationName = "Learning App";
// Confirm that the static block has executed
System.out.println("Static block executed."); // Output :- Static block executed.
}
}
public class Main {
public static void main(String[] args) {
// Accessing the class causes its static initialization
System.out.println(Configuration.applicationName); // Output :- Learning App
}
}

Static blocks are useful when initializing static variables requires calculations or multiple statements.

Static Block

Static Block

Static vs Non-Static Members

Static Members

Non-Static Members

Belong to the class

Belong to individual objects

Shared by all objects

Each object has its own copy

Accessed using the class name

Accessed using an object

Do not require object creation

Require an object for access

Cannot directly access instance members

Can access static and instance members

Advantages of the Static Keyword

  • Shares common data among objects

  • Allows methods to run without object creation

  • Provides one-time class initialization

  • Helps create utility methods

  • Can be used to define shared counters and constants

Summary

The static keyword in Java creates members that belong to a class rather than its objects.

A static variable stores shared data, a static method can run without an object, and a static block performs one-time initialization of static members.

CS Core

Read Similar Blogs

Comments0