Table of contents
1.
Introduction
2.
Static and Non-static Nested Classes Differences
3.
Example of Static and Non-Static Nested Classes
3.1.
1. Static Nested Class Example
3.2.
2. Non-Static Nested Class (Inner Class) Example
3.3.
3. Combined Example Showing Both Types
4.
Frequently Asked Questions
4.1.
Can a nested class (static one) access non-static members of the outer?
4.2.
Why should I use a static nested class as opposed to a normal inner class?
4.3.
May non-static methods exist in a static nested class?
5.
Conclusion
Last Updated: Jun 23, 2025
Easy

Static Class in Java

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

There are various developments to structure the code in Java, and one method is through usage of classes that are static classes. A nested class that is declared using the static keyword is a static class. A static class, unlike ordinary inner classes, does not require a corresponding instance of the surrounding class to be instantiated. It is a standalone entity and can refer to static members of the outer class. 

In this article, we shall learn what static classes are, how they differ from non-static nested classes, and where they are applicable. We are also going to consider practical examples so as to examine how they can operate in a real code. 

Static and Non-static Nested Classes Differences

AspectStatic Nested ClassNon-static Nested Class (Inner Class)
DefinitionA nested class defined using the static keyword; it does not require an instance of the outer class.A nested class that must be associated with an instance of the outer class.
Outer Class Members AccessCan only access static members of the outer class directly.Can access both static and non-static members (including private) of the outer class.
Instance CreationCan be instantiated without an instance of the outer class.Requires an instance of the outer class to be instantiated.
Memory UsageConsumes less memory as it has no implicit reference to the outer class.Consumes more memory due to an implicit reference to the outer class.
Instantiation SyntaxStandard instantiation like: OuterClass.StaticNestedClass obj = new OuterClass.StaticNestedClass();Requires outer class instance: OuterClass outer = new OuterClass(); OuterClass.InnerClass obj = outer.new InnerClass();
AccessibilityCan have any access modifier (public/protected/private/package-private), independent of the outer class.Can also have any access modifier, but access is influenced by outer class visibility.
Inheritance CapabilityCan extend classes and implement interfaces without restrictions.Can also extend classes and implement interfaces, but remains associated with the outer class.
Use CasesBest for utility/helper classes that don't need access to instance data of the outer class.Best when tight coupling to outer class instance is required—typically when inner class uses outer class methods or fields heavily.
Compilation OutputCompiled as a separate file like: OuterClass$StaticNestedClass.classCompiled as a separate file like: OuterClass$InnerClass.class, with reference to outer class.
Performance ImplicationsTypically better performance due to no outer class reference and reduced overhead.Might have slightly lower performance due to outer class reference overhead.
Garbage CollectionCan be garbage-collected independently of the outer class instance.May cause memory leaks if outer class instance cannot be garbage collected due to reference by inner class.
Thread SafetyInherits no thread safety issues from outer class. Thread safety must be managed explicitly.Shares thread safety risks with the outer class due to shared access to its instance members.

Example of Static and Non-Static Nested Classes

Let's look at real code examples to understand how static and non-static nested classes work in practice. We'll create an outer class called University with both types of nested classes.

1. Static Nested Class Example

public class University {
    // Static variable of outer class
    static String universityName = "Tech University";
    
    // Static nested class
    static class Department {
        void display() {
            // Can only access static members of outer class
            System.out.println("Department of " + universityName);
        }
    }


    public static void main(String[] args) {
        // Creating object of static nested class
        University.Department dept = new University.Department();
        dept.display();  // Output: Department of Tech University
    }
}
You can also try this code with Online Java Compiler
Run Code


Key Points:

  • We created the Department static nested class without needing a University object
     
  • The display() method can only access the static universityName variable
     
  • Object creation is simple: University.Department dept = new University.Department();

2. Non-Static Nested Class (Inner Class) Example

public class University {
    // Instance variable of outer class
    String universityName = "Tech University";
    
    // Non-static inner class
    class Student {
        void display() {
            // Can access both static and non-static members
            System.out.println("Student of " + universityName);
        }
    }


    public static void main(String[] args) {
        // First create outer class object
        University uni = new University();
        
        // Then create inner class object
        University.Student student = uni.new Student();
        student.display();  // Output: Student of Tech University
    }
}
You can also try this code with Online Java Compiler
Run Code


Key Points:

  • We needed a University object (uni) before creating the Student inner class object
     
  • The inner class can access the instance variable universityName
     
  • Object creation requires two steps: first outer object, then inner object

3. Combined Example Showing Both Types

public class Computer {
    // Outer class variable
    String brand = "TechComp";
    static String type = "Laptop";
    
    // Static nested class
    static class Processor {
        void show() {
            System.out.println("Processor for " + type);  // Can access static 'type'
            // System.out.println(brand);  // Error - can't access non-static
        }
    }
    
    // Non-static inner class
    class RAM {
        void display() {
            System.out.println("RAM for " + brand);  // Can access non-static
            System.out.println("Device type: " + type);  // Can also access static
        }
    }


    public static void main(String[] args) {
        // Static nested class usage
        Computer.Processor cpu = new Computer.Processor();
        cpu.show();
        
        // Non-static inner class usage
        Computer comp = new Computer();
        Computer.RAM memory = comp.new RAM();
        memory.display();
    }
}
You can also try this code with Online Java Compiler
Run Code


Output:

Processor for Laptop
RAM for TechComp
Device type: Laptop

Frequently Asked Questions

Can a nested class (static one) access non-static members of the outer?

No, a nested class that is not dynamic can only access the static members (variables & methods) of the outside class. It will result in a compilation error when it attempts to access non-static members.

Why should I use a static nested class as opposed to a normal inner class?

Choose a static nested class in the case that the nested class has no use cases of accessing instance variables of the outer class. This makes the code use less memory to store a reference to the outer class.

May non-static methods exist in a static nested class?

Yes, a non-static method may be included in a static nested class. But still it has no access to non-static members of the outer class.

Conclusion

We have come to understand in this article about static classes in Java. We observed the difference between them in creation of objects and the use of memory and in accessing members of outer classes over the non-static nested classes. We also had a practical example to see how static and non-static nested classes work in practice.We find useful a static nested class when we want to have a helper class but have no need to refer to instances of the outer class, and a non-static inner class when we want full access to instance variables of the outer class. The selection of the proper type assists in the creation of a cleaner and more productive code.

Live masterclass