His
His

Reputation: 6043

When would I want to make my private class static?

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

Upvotes: 77

Views: 69109

Answers (6)

stelios.anastasakis
stelios.anastasakis

Reputation: 1196

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.

Upvotes: 5

Deiwin
Deiwin

Reputation: 447

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

Upvotes: 1

Esko Piirainen
Esko Piirainen

Reputation: 1379

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.

Upvotes: 52

Paul
Paul

Reputation: 598

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.

Upvotes: 3

Sergey
Sergey

Reputation: 11908

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

Upvotes: 0

Jeff Storey
Jeff Storey

Reputation: 57192

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.

Upvotes: 5

Related Questions