SrcForge

Lesson 26

Inner Classes, Static Members and Static Blocks

Inner classes organize code inside other classes or methods. Static variables, methods and blocks belong to the class rather than any object.

Java Track

Save progress as you learn.

61 lessons

Lesson content

Inner Classes in Java allow you to define a class inside another class or method. They help organize code, improve encapsulation and enable access to members of the enclosing class.

Types of Inner Classes

  • Nested Inner Class
  • Local Inner Class
  • Anonymous Inner Class
  • Static Inner Class

Definition List

  • Inner Class: A class declared inside another class.
  • Nested Inner Class: A non-static class defined inside another class.
  • Local Inner Class: A class declared inside a method.
  • Anonymous Inner Class: An inner class without a name that is created and used immediately.
  • Static Inner Class: A nested class declared with the static keyword.
  • Static Variable: A variable shared by all objects of a class.
  • Static Method: A method that belongs to the class rather than an object.
  • Static Block: A block of code executed once when the class is loaded into memory.

Nested Inner Class in Java

A Nested Inner Class is a class declared inside another class. It can directly access the members of the outer class.

Syntax

class OuterClass {
    class InnerClass {
        // code
    }
}

Example 01: Nested Inner Class

class outer {
    int a = 50;

    class inner {
        int b = 58;

        void innerDisplay() {
            System.out.println("A : " + a);
            System.out.println("B : " + b);
        }
    }

    void outerDisplay() {
        inner i = new inner();
        i.innerDisplay();
        System.out.println("B from inner Class by Outer Display : " + i.b);
    }
}

public class nestedClass {
    public static void main(String args[]) {
        outer o = new outer();
        o.outerDisplay();

        outer.inner i = new outer().new inner();
        i.innerDisplay();
    }
}

Key Points

  • Defined inside another class.
  • Can access outer class variables and methods.
  • Requires an object of the outer class.

Local Inner Class in Java

A Local Inner Class is a class declared inside a method. It can only be used within that method.

Syntax

class OuterClass {
    void method() {
        class InnerClass {
            // code
        }
    }
}

Example 02: Local Inner Class

class Outercls {
    void display() {
        class Inner {
            void innerDisplay() {
                System.out.println("Inner Display");
            }
        }

        Inner i = new Inner();
        i.innerDisplay();
    }
}

public class localInnerClass {
    public static void main(String[] args) {
        Outercls o = new Outercls();
        o.display();
    }
}

Output

Inner Display

Static Inner Class in Java

A Static Inner Class is a nested class declared using the static keyword. Unlike regular inner classes, it does not require an object of the outer class.

Example 04: Static Inner Class

class OuterClass {
    static int x = 10;
    int y = 20;

    static class InnerClass {
        void display() {
            System.out.println("X : " + x);
        }
    }
}

public class staticInnerClass {
    public static void main(String[] args) {
        OuterClass.InnerClass i = new OuterClass.InnerClass();
        i.display();
    }
}

Output

X : 10

Static Variables in Java

A variable declared with the static keyword becomes a static variable. Static variables are shared among all objects of a class.

Features of Static Variables

  • Common for all objects.
  • Memory is allocated only once.
  • Accessed using the class name.

Example 05: Static Variables and Methods

class staticTest {
    static int a = 10;
    int b = 20;

    void show() {
        System.out.println("A : " + a + " B : " + b);
    }

    static void display() {
        System.out.println("A : " + a);
    }
}

public class stat_vari_methods {
    public static void main(String args[]) {
        staticTest o1 = new staticTest();
        o1.show();

        staticTest o2 = new staticTest();
        o2.b = 100;

        staticTest.a = 200;
        o2.show();
        o1.show();
    }
}

Static Methods in Java

A method declared with the static keyword belongs to the class rather than an object.

Rules of Static Methods

  • Can access only static members directly.
  • Cannot access instance variables directly.
  • Can be called using the class name.
  • No object creation is required.

Static Blocks in Java

Static blocks are special blocks of code that execute once when the class is loaded into memory. They execute before the main() method.

Example 07: Static Blocks

class BlockTest {
    static {
        System.out.println("BlockTest-1");
    }

    static {
        System.out.println("BlockTest-2");
    }
}

public class staticBlocks {
    static {
        System.out.println("Block-1");
    }

    public static void main(String[] args) {
        BlockTest o = new BlockTest();
        System.out.println("Main");
    }

    static {
        System.out.println("Block-2");
    }
}

Output

Block-1
Block-2
BlockTest-1
BlockTest-2
Main

FAQ

1. What is the difference between an inner class and a static inner class?

An inner class requires an object of the outer class, while a static inner class can be accessed without creating an outer class object.

2. Why are static variables shared among all objects?

Static variables belong to the class rather than individual objects, so only one copy exists for all instances.

3. When are static blocks executed in Java?

Static blocks execute once when the class is loaded into memory, before the main() method runs.

Inner class is defined inside another class.
Local inner class is declared inside a method.
Static inner class needs no outer class object.
Static variables are shared by all objects.
Static methods belong to the class, not objects.
Static blocks run before the main() method.

Where Java is used

Java in real-world development

Nested Inner Class

Declared inside another class. Can access outer class members directly.

Local Inner Class

Declared inside a method. Only usable within that method.

Static Inner Class

Declared with static keyword. No outer class object needed.

Static Variable

Shared across all objects. One memory allocation for the entire class.

Static Block

Runs once on class load. Executes before the main() method.

Prev: InterfaceBack to hub