74

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
{
    ...
}
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
His
  • 5,891
  • 15
  • 61
  • 82
  • 1
    The [java-inner-class-and-static-nested-class](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class) maybe helpful. – MockerTim Jun 03 '11 at 05:05
  • You may find the second answer in this post helpful: http://stackoverflow.com/questions/1844355/java-static-class – gwood Jun 03 '11 at 05:06

6 Answers6

52

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.

Esko Piirainen
  • 1,296
  • 2
  • 14
  • 28
5

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.

4

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.

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
  • 1
    This is poor answer. Even from a static class, if i get reference to object of the parent class, i can access the private members. – Op De Cirkel Jun 03 '11 at 05:11
  • But why would you? If you need access to the private variables and it is an inner class, just make it non-static instead of making it static and passing a reference to the parent. – Jeff Storey Jun 03 '11 at 05:27
  • They are different concepts. non-static (i.e. inner) classes have implicit reference to object of the parent class. So, you can not have object of the inner class if there is no object of the parent class. An example of this would be Map.Entry (or better Iterator) - it needs reference to the parent map object. an example of static nested class would be Builder, it needs access to parent's private constructor – Op De Cirkel Jun 03 '11 at 05:33
  • But these classes (Map.Entry for example) are not private. The OP asked about private classes. – Jeff Storey Jun 03 '11 at 12:20
  • @Jeff Storey, perhaps the interface is not private, but the implementation is possible and good to be private/protected. i.e. When you say myList.getIterator(), you are getting some implementation of the Iterator iface that is private or protected – Op De Cirkel Jun 03 '11 at 12:54
3

I would assume you are referring to inner classes.

I think the motivation would be coming from how you want to associate your inner class. If you want your inner class to be associated to a specific instance of its outer class, you'd use private class, otherwise, use private static class.

Paul
  • 588
  • 1
  • 4
  • 16
1

I found it useful in having a specific exception in a generic abstract class. I.e.:

public abstract class AbstractClass <T>
{
    private void doSomethingOrThrowException() throws SpecificException
    {
        ....

        if ( ! successful)
        {
            throw new SpecificException();
        }
    }

    private static class SpecificException extends Exception {}
}

If I were to leave out the static, the compiler would give me an error that states: The generic class AbstractClass<T>.SpecificException may not subclass java.lang.Throwable

Deiwin
  • 420
  • 5
  • 21
0

static classes differ from ordinary classes only in that they can be accessed without their instances being created. so if you need some class to be accessable every time, use static

Sergey
  • 11,548
  • 24
  • 76
  • 113