简体   繁体   English

为什么我要将私有构造函数与静态嵌套类组合在一起?

[英]Why would I combine a private constructor with a static nested class?

I'm currently digging a little bit into accessibility of Java classes. 我正在挖掘Java类的可访问性。 While there is a varity of possibilities to define classes , I wonder about a use case for the example below. 虽然定义类有多种可能性,但我想知道下面示例的用例。

Basically, the constructor of AnotherClass is private. 基本上, AnotherClass的构造AnotherClass是私有的。 However, AnotherClass has a static nested class , which is accessible within the PublicClass class. 但是, AnotherClass有一个静态嵌套类 ,可以在PublicClass类中访问。

It's just something I came up with out of curiosity, but as it actually works, I wonder, why would I ever use something like this? 这只是我出于好奇而想出来的东西,但是因为它实际上有效,我想知道,为什么我会使用这样的东西?

Example

public class PublicClass {  
    public PublicClass() {
        AnotherClass.AnotherInnerClass innerClass = new AnotherClass.AnotherInnerClass();
        innerClass.anotherTest();
    }
}


class AnotherClass{
    /**
     * Private constructor - class cannot be instantiated within PublicClass.
     */
    private AnotherClass(){

    }

    /**
     * Static inner class - can still be accessed within package.
     */
    static class AnotherInnerClass{
        public void anotherTest(){
            System.out.println("Called another test.");
        }
    }
}

Note those classes are within the same file. 请注意,这些类在同一个文件中。

Output 产量

Called another test.

The AnotherInnerClass CAN use the private constructor of AnotherClass . AnotherInnerClass使用AnotherInnerClass的私有构造AnotherClass This is used for example in the Builder pattern , which is something along the lines of this: 例如,在Builder模式中使用它 ,这是这样的:

public class Foo {  
    public Foo() {
        Bar.Builder barBuilder = new Bar.Builder();
        Bar bar = barBuilder.build();
    }
}


public class Bar{
    private Bar(..){

    }

    static class Builder{
        public Bar build(){
            return new Bar(..);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 静态嵌套类可以访问外部类的私有构造函数 - Static nested class has access to private constructor of outer class 使用公共构造函数 Java 声明一个私有 static 嵌套 class? - Declare a private static nested class with a public constructor Java? 为什么我要使用类变量(例如:static private int n),然后为它使用 getter 和 setter? - Why would I use a class variable (ex: static private int n) and then use a getter and a setter for it? 我什么时候想要我的私人 class static? - When would I want to make my private class static? 当我将嵌套的静态Fragment类声明为private时,为什么会生成错误? - Why is an error generated when I declare a nested static Fragment class as private? 如何使用私有构造函数在 class 中测试此 static 成员? - How do I test this static member in a class with private constructor? 嵌套类中私有构造函数的范围 - scope of private constructor in Nested Class 为什么要使用私有静态内部类? 破坏了静态内部类的目的? - Why would you use private static inner class? Defeats the purpose of static inner class? 如何实例化嵌套的私有静态类 java - How do I instantiate a nested private static class java 如何使用PowerMockito在具有私有构造函数的类中设置原始私有静态final字段? - How do I set a primitive private static final field in a class with private constructor using PowerMockito?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM