What is the difference between an inner and nested class in Java? What about the difference between an inner class and a static inner class?

This tutorial is a bit complex, but we’ll try to keep things as simple as possible.
With that said, let’s start off with definition and explanation of nested classes.

Nested classes can be either static or non-static

Nested classes can be further classified into two different types of classes: non-static nested classes and static nested classes. Non-static nested classes are more formally known as inner classes. So, think of nested classes as a big container with 2 smaller boxes inside – 1 box is for static nested classes, and another box is for inner classes (also known as non-static nested classes).

Example of an inner class (aka non static nested class)

Here’s a simple example of an inner class – where InnerClass is the inner class:

class OuterClass  {
/*  some code here...*/

class InnerClass  {  }

/*  some code here...*/
}

Note in the code above that InnerClass is literally declared inside the OuterClass class.

Inner classes are subsets of nested classes

Remember that an inner class is a specific type of nested class that occurs when a nested class is non-static. And that is the “difference” between an inner class and a nested class – in other words, inner classes are subsets of nested classes. So, be careful, because the terms “inner class” and “nested class” are NOT interchangeable. You can say that an inner class is also a nested class, but you can NOT say that a nested class is also an inner class. This is because nested classes are part of the larger set that includes both inner classes and static nested classes.

What’s so special about inner classes?

So, what exactly is special about inner classes? Well, the main thing that you must remember about inner classes is that an instance of an inner class has access to all of the members of the outer class, even those that are marked “private”. So, when an instance of the inner class is created, there are no issues with having the inner class access the instance variables of the outer class.

Inner class versus static inner classes

Before we dive into the differences between inner classes and static inner classes, the most important thing you should know is that static inner classes is the wrong terminology – they should be called static nested classes instead.

Why you must use static “nested” classes instead of static “inner” classes

There is no such thing as a static inner class, because the term “inner class” means that the inner class has access to the instance variables of the outer class.

An inner class is part of the “inner circle” of the outer class

Think of an inner class as being part of the “inner circle” of the outer class – an inner class instance can access all the members of the outer class, even the ones declared private. But, if an inner class were also declared to be static it would be impossible to have access to all of the members of the outer class – think about why on your own before you read our answer. You should be able to come up with an answer on your own as long as you know what static means.

Inner classes have access even to non-static members of outer class

The reason an inner class can’t be static is because of the fact that it’s impossible to access non-static variables and methods from within a static context. So, a static inner class would only have access to the static members of the outer class. And, by definition, an inner class should have access even to the non-static members of the outer class. So, instead of calling them static inner classes, we call them static nested classes, and remember that the reason is that ‘inner’ classes have a special relationship with the outer class. And to call it a static inner class would be a misuse of the terminology – so they are called static nested classes instead.

Example of a static nested class

Here’s a simple example of a static nested class:

class Outer {
    static class NestedStatic { }
}

The correct way of thinking about the NestedStatic class in our example above is that it is a static member of the outer class. And because it is a static member, it means that it can be accessed without an instance of the Outer class.

How to instantiate a static nested class

The syntax used to instantiate a static nested class is different depending on whether the nested static class is a member of the current class or if the static nested class is nested in some other class. Confused? Some examples will help clarify what we mean:

Instantiating a static nested class from a non-enclosing class

Suppose we want to create an instance of a static nested class from another class. Here is some code where we do that – note that in the NonEnclosingClass class we instantiate the static class called Nested that is a member of the EnclosingClass class.

class EnclosingClass {
  static class Nested {
  void someMethod() { System.out.println("hello"); }
  }
}

class NonEnclosingClass {

public static void main(String[] args) {
/*instantiate the Nested class that is a static
  member of the EnclosingClass class:
*/

EnclosingClass.Nested n = new EnclosingClass.Nested(); 

n.someMethod();  //prints out "hello"
}
}

Instantiating a static nested class from an enclosing class

Here you can see an example of how to instantiate a static inner class that is already a part of the current class – note the more ‘normal’ syntax that you’re probably used to seeing of “Nested n = new Nested();” versus “EnclosingClass.Nested n = new EnclosingClass.Nested();” which is what was used to instantiate a static class that’s a member of a different class, as we showed in the example above.


class EnclosingClass {

static class Nested {
void anotherMethod() { System.out.println("hi again"); } 
}

public static void main(String[] args) {
//access enclosed class:

Nested n = new Nested(); 

n.anotherMethod();  //prints out "hi again"
}

}

Accessing non-static instance variables from a static nested class

Now, let’s see what happens if we try to access some non-static instance variables that belong to the Outer class from inside of the NestedStatic class:

class Outer {
    int instanceVar = 5;
    
    static class NestedStatic {
    public static void main(String[] args){
    
    /*instanceVar is a non-static variable
    belonging to the Outer class:  */
    
    instanceVar = 10; 
    
    }
    }
}

The code above throws a compiler error saying “non-static variable instanceVar cannot be referenced from a static context”. That error makes perfect sense because we are trying to access instanceVar, which is non-static, from the NestedStatic class, which is static. And, because of the fact that we can access the static NestedStatic class without an object of the Outer class, it makes sense that we should not be able to access an instance variable like instanceVar (which needs an object of the Outer class in order to be accessed since it’s not static).

Conclusion

If you are looking for another interesting article on inner classes, then check out: When to use inner classes in Java

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.

10 thoughts on “Inner vs nested class”