开发者

When would I want to make my private class static?

开发者 https://www.devze.com 2023-03-10 07:01 出处:网络
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?

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 st开发者_开发技巧atic class Foo
{
    ...
}

vs

private class Foo
{
    ...
}


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.


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消