When would I want to make my private class static?

JavaClassStaticPrivate

Java Problem Overview


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
{
    ...
}

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

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.

Solution 4 - Java

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.

Solution 5 - Java

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

Solution 6 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHisView Question on Stackoverflow
Solution 1 - JavaEsko PiirainenView Answer on Stackoverflow
Solution 2 - Javastelios.anastasakisView Answer on Stackoverflow
Solution 3 - JavaJeff StoreyView Answer on Stackoverflow
Solution 4 - JavaPaulView Answer on Stackoverflow
Solution 5 - JavaDeiwinView Answer on Stackoverflow
Solution 6 - JavaSergeyView Answer on Stackoverflow