When would I want to make my private class static?

His picture His · Jun 3, 2011 · Viewed 45.7k times · Source

In general, are there any benefits in declaring a private class as static?

In what cases would I want to use one of the following over the other?

private static class Foo
{
    ...
}

vs

private class Foo
{
    ...
}

Answer

Esko Piirainen picture Esko Piirainen · Feb 28, 2014

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.