πŸŽ‰ Special Offer !    Code: GET300OFF    Flat β‚Ή300 OFF on every Java Course
Grab Deal πŸš€

"static" Keyword in Java  


Introduction
  • static keyword is a non-access modifier in Java.
  • It is used to create members (variables, methods, blocks or nested classes) that belong to the class rather than any specific object.
  • We don't need to create an object to access a static member β€” we can access it using the class name directly.
  • Use of static keyword:
    • It is used for memory management by sharing data among all instances of a class.
    • It means a static member is shared among all objects of the class.

1. Static Variable (Class Variable):
  • A static variable is shared among all objects of a class.
  • It is initialized only once, at the start of program execution.
  • Useful for defining common properties or counters.
  • Java Program Example 1:
    class Student
    {
        String name;
        int rollNo;
        static String schoolName = "ABC Public School";  // static variable shared by all students
    
        // Constructor
        Student(String name, int rollNo)
        {
            this.name = name;
            this.rollNo = rollNo;
        }
    
        // Method to display student details
        void displayDetails()
        {
            System.out.println("Name     : " + name);
            System.out.println("Roll No  : " + rollNo);
            System.out.println("School   : " + schoolName);
            System.out.println("--------------------------");
        }
    }
    
    public class StaticDemo
    {
        public static void main(String[] args)
        {
            // Creating student objects
            Student s1 = new Student("Amit", 101);
            Student s2 = new Student("Deepak", 102);
            Student s3 = new Student("Rahul", 103);
    
            // Displaying student details
            s1.displayDetails();
            s2.displayDetails();
            s3.displayDetails();
        }
    }
  • Java Program Example 2:
    public class Counter
    {
        // static variable to keep count of objects
        static int count = 0;
    
        // Constructor
        Counter()
        {
            count++;  // Increment static counter
            System.out.println("Object created. Count = " + count);
        }
    
        public static void main(String[] args)
        {
            // Creating objects
            Counter c1 = new Counter();
            Counter c2 = new Counter();
            Counter c3 = new Counter();
        }
    }

2. Static Method:
  • A static method belongs to the class, not to any specific object.
  • It can be called without creating an object of the class.
  • Java Program Example:
    public class StaticDemo
    {
        // Static method
        static void greet()
        {
            System.out.println("Hello! This is a static method.");
        }
    
        // Non-static method
        void showMessage()
        {
            System.out.println("This is a non-static method.");
        }
    
        public static void main(String[] args)
        {
            // Calling static method directly without creating an object
            greet();
    
            // Calling non-static method requires an object
            StaticDemo obj = new StaticDemo();
            obj.showMessage();
        }
    }

3. Static Block:
  • A static block is used to initialize static variables.
  • It is executed only once, when the class is first loaded into memory.
  • It is useful for setting up complex static values.
  • Java Program Example:
    public class StaticDemo
    {
        static int maxLimit;
        static
        {
            maxLimit = 100;  // static block for initialization
            System.out.println("Static block executed.");
        }
    
        public static void main(String[] args)
        {
            System.out.println("Max Limit : "+maxLimit);
            System.out.println("Main method executed");
        }
    }

4. Static Class (Nested Class):
  • Java allows the creation of static nested classes (a class inside another class declared as static).
  • A static nested class can access only static members of the outer class.
  • It doesn't need an instance of the outer class to be instantiated.
  • Java Program Example:
    public class Outer
    {
        // Static nested class
        static class Inner
        {
            void show()
            {
                System.out.println("Static nested class method.");
            }
        }
    
        public static void main(String[] args)
        {
            // Creating an object of the static nested class
            Outer.Inner obj = new Outer.Inner();
            obj.show();
        }
    }