CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Understanding the static Keyword in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

Understanding the static Keyword in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 3 min read ยท 0 Comments
Share: in X

The static keyword in Java is one of the fundamental concepts that every developer must understand. It is used for memory management and allows variables, methods, blocks, and nested classes to be associated with the class rather than with a specific instance. This makes static a powerful tool for optimizing performance and organizing code efficiently.

In this blog post, we will explore the static keyword in Java, its use cases, and examples to demonstrate how and when to use it.

Table of Contents

  • What is the static Keyword?
  • Use Cases of static in Java
    • 1. Static Variables (Class Variables)
    • 2. Static Methods
    • 3. Static Blocks
    • 4. Static Nested Classes
  • When to Use static in Java?
  • Key Takeaways
  • Conclusion

What is the static Keyword?

In Java, the static keyword is a non-access modifier used for:

  • Static Variables (Class Variables)
  • Static Methods
  • Static Blocks
  • Static Nested Classes

When a member is declared as static, it belongs to the class rather than any specific object. This means that all instances of the class share the same static member.

Use Cases of static in Java

1. Static Variables (Class Variables)

A static variable is shared among all instances of the class. It is useful for defining common properties shared by all objects of a class.

Example:

class Employee {
    static String company = "CoderSathi"; // Static variable
    int id;
    String name;
    
    Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }

    void display() {
        System.out.println("ID: " + id + ", Name: " + name + ", Company: " + company);
    }
}

public class StaticExample {
    public static void main(String[] args) {
        Employee emp1 = new Employee(101, "Radha");
        Employee emp2 = new Employee(102, "Krishnan");
        
        emp1.display();
        emp2.display();
    }
}

Output:

ID: 101, Name: Radha, Company: CoderSathi
ID: 102, Name: Krishnan, Company: CoderSathi

2. Static Methods

Static methods belong to the class and do not require an instance to be called. They can only access static data.

Example:

class Utility {
    static int square(int num) {
        return num * num;
    }
}

public class StaticMethodExample {
    public static void main(String[] args) {
        int result = Utility.square(9);
        System.out.println("Square of 9: " + result);
    }
}

Output:

Square of 9: 81

3. Static Blocks

Static blocks are executed once when the class is loaded into memory. They are useful for initializing static variables.

Example:

class DatabaseConnection {
    static String url;

    static {
        url = "jdbc:mysql://localhost:3306/mydb";
        System.out.println("Static block executed: Database URL initialized");
    }
}

public class StaticBlockExample {
    public static void main(String[] args) {
        System.out.println("Database URL: " + DatabaseConnection.url);
    }
}

Output:

Static block executed: Database URL initialized
Database URL: jdbc:mysql://localhost:3306/mydb

4. Static Nested Classes

Static classes are nested within another class and do not require an instance of the outer class.

Example:

class OuterClass {
    static class StaticNestedClass {
        void display() {
            System.out.println("Inside static nested class");
        }
    }
}

public class StaticNestedClassExample {
    public static void main(String[] args) {
        OuterClass.StaticNestedClass obj = new OuterClass.StaticNestedClass();
        obj.display();
    }
}

Output:

Inside static nested class

When to Use static in Java?

Use the static keyword when:

  • A variable or method should be shared among all instances of a class (e.g., database connections, constants).
  • A method does not depend on instance variables and can work only with static data.
  • Initializing data once at the time of class loading is required (static block).
  • Creating utility/helper classes that do not need object instantiation.
  • Defining nested classes that do not require an instance of the outer class.

Key Takeaways

  • static members belong to the class, not individual instances.
  • Static variables are shared among all objects of a class.
  • Static methods can only access static data and cannot use this keyword.
  • Static blocks execute once when the class is loaded.
  • Static nested classes can be instantiated without creating an instance of the outer class.

Conclusion

The static keyword in Java is a powerful feature that helps in memory management and efficient coding. By using static appropriately, developers can optimize performance, avoid redundant object creation, and keep their code clean and organized.

Do you use static frequently in your Java projects? Share your thoughts and experiences in the comments below!

Related Posts:

  • Nested and Inner Class in Java
  • Control Statements in Java
  • Packages in Java: A Guide to Modular, Maintainable Code
  • Java Classes and Objects: The Foundation of Object…
  • final Keyword in Java
  • Identifiers in Java
Tags:javalanguage-fundamentals
Was this article helpful?
โ† Previous ArticleAbstract Class vs Interface: Side-by-Side Comparison
Next Article โ†’Method Overloading vs Method Overriding: Understanding Core OOP Concepts

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap