我什么时候想让我的私有类静态?

时间:2022-10-19 22:32:56

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

VS

private class Foo
{
    ...
}

5 个解决方案

#1


30  

I think this is a good starting point: http://java67.blogspot.fi/2012/10/nested-class-java-static-vs-non-static-inner.html

我认为这是一个很好的起点: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.

1)嵌套静态类不需要引用外部类,但非静态嵌套类或内部类需要外部类引用。如果不创建Outer类的实例,则无法创建Inner类的实例。在使嵌套类静态或非静态时,这是最重要的考虑因素。

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.

2)static 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.

3)静态和非静态嵌套类之间的另一个区别是你不能访问非静态成员,例如方法和字段直接嵌入到静态类中。如果这样做,您将收到类似“非静态成员不能在静态上下文中使用”的错误。而Inner类可以访问Outer类的静态和非静态成员。

#2


2  

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.

如果需要访问封闭类的成员变量/方法,请使用非静态表单。如果不这样做,请使用静态表单。

#3


1  

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.

我认为动机来自你想要如何关联你的内心阶级。如果希望内部类与其外部类的特定实例相关联,则使用私有类,否则使用私有静态类。

#4


0  

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

如果我要省略静态,编译器会给我一个错误,指出:泛型类AbstractClass .SpecificException可能不是java.lang.Throwable的子类

#5


-1  

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

静态类与普通类的不同之处仅在于它们可以在不创建实例的情况下访问它们。因此,如果您每次都需要某些类可访问,请使用static

#1


30  

I think this is a good starting point: http://java67.blogspot.fi/2012/10/nested-class-java-static-vs-non-static-inner.html

我认为这是一个很好的起点: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.

1)嵌套静态类不需要引用外部类,但非静态嵌套类或内部类需要外部类引用。如果不创建Outer类的实例,则无法创建Inner类的实例。在使嵌套类静态或非静态时,这是最重要的考虑因素。

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.

2)static 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.

3)静态和非静态嵌套类之间的另一个区别是你不能访问非静态成员,例如方法和字段直接嵌入到静态类中。如果这样做,您将收到类似“非静态成员不能在静态上下文中使用”的错误。而Inner类可以访问Outer类的静态和非静态成员。

#2


2  

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.

如果需要访问封闭类的成员变量/方法,请使用非静态表单。如果不这样做,请使用静态表单。

#3


1  

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.

我认为动机来自你想要如何关联你的内心阶级。如果希望内部类与其外部类的特定实例相关联,则使用私有类,否则使用私有静态类。

#4


0  

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

如果我要省略静态,编译器会给我一个错误,指出:泛型类AbstractClass .SpecificException可能不是java.lang.Throwable的子类

#5


-1  

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

静态类与普通类的不同之处仅在于它们可以在不创建实例的情况下访问它们。因此,如果您每次都需要某些类可访问,请使用static