When would I want to make my private class static?

I think this is a good starting point: http://java67.blogspot.fi/2012/10/nested-class-java-static-vs-non-static-inner.html

1) Nested static class doesn't need reference of Outer class but non static nested class or Inner class requires Outer class reference. You can not create instance of Inner class without creating instance of Outer class. This is by far most important thing to consider while making a nested class static or non static.

2) static class is actually static member of class and can be used in static context e.g. static method or static block of Outer class.

3) Another difference between static and non static nested class is that you can not access non static members e.g. method and field into nested static class directly. If you do you will get error like "non static member can not be used in static context". While Inner class can access both static and non static member of Outer class.


If i understand correctly, the question is for private class vs private static class. All the responses are generally about inner classes, that are not 100% applied to that question. So first things first:

From geeksforgeeks:

  • Nested class -> a class within another class
  • static nested class -> Nested classes that are declared static are called static nested classes
  • inner class -> An inner class is a non-static nested class.

As the accepted response says, static vs non-static nested classes differ on the way and possibility to access methods/fields outside the outer class. But in case of private classes B within class A, you dont have this issue, cause B is not accessible outside A anyway.

Now, from inside class A, for non-static fields/methods you can always refer to class B, either by saying new A.B() or just new B() and it doesnt matter (no compilation/runtime errors) if B is private class or private static class. In case of static fields/methods you need to use a private static class.

Moreover, if you want to access from inside B a non-static field of A, then you can't have B as private static class.

I generally prefer private static class, except when i cant use it like in the previous case, cause intellij will give warnings otherwise.


If you need access to the member variables/methods of the enclosing class, use the non-static form. If you don't, use the static form.