简体   繁体   English

为什么我们在java中声明嵌套的公共静态类,即使它也被用在其他一些地方呢?

[英]Why we declare the nested public static class in java, even though its being used in some other places as well?

I was going through the legacy code base and observed that so many places they are using the public static class inside an outer class and nested public static class is not just being used in the outer class but its being used in so many other class? 我正在浏览遗留代码库并观察到很多地方他们在外部类中使用public static类,而嵌套的公共静态类不只是在外部类中使用,而是在其他许多类中使用它?

What would be the design decision behind that and if it being used outside as well then why it wasn't created as a standalone public class in the first place itself. 背后的设计决策是什么,如果它在外面使用,那么为什么它本身不是作为一个独立的公共类创建的。

So in my example it looks like below :- 所以在我的例子中它看起来如下: -

public class OuterNestedClass {
    private int a;
    private List<InnerClass> innerClasses;

    public static class InnerClass {
        private int b;

        public InnerClass(int b) {
            this.b = b;
        }

        public void show(){
            System.out.println("Value of b "+b);
        }
    }
}

And other class which uses the innerclass looks like below :- 使用innerclass类的其他类如下所示: -

public class OutsideClass {
    public static void main(String[] args) {
        OuterNestedClass.InnerClass innerClass = new OuterNestedClass.InnerClass(10);
        innerClass.show();
    }
}

Let me know if any clarification is required. 如果需要澄清,请告诉我。

Main reason for that would be namespacing. 主要原因是命名空间。 Static classes are utility classes, and those tend to grow very large. 静态类是实用程序类,它们往往会变得非常大。 Nested classes let you break your utilities nicely, while still keeping everything together. 嵌套类可以让你很好地破坏你的实用程序,同时仍然保持一致。

So, instead of having Utils with 20 methods, you probably have Utils.Password with 5 and Utils.Json with 15 因此,您可能拥有带有5的Utils.Password和带有15的Utils.Json,而不是使用带有20种方法的Utils

Best example for that I've seen is how Retrofit.Builder is done: https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit2/Retrofit.java#L394 我见过的最好的例子是Retrofit.Builder是如何完成的: https//github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit2/Retrofit.java#L394

暂无
暂无

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

相关问题 使用公共构造函数 Java 声明一个私有 static 嵌套 class? - Declare a private static nested class with a public constructor Java? 为什么我们被限制在java内部类中声明静态成员变量? - Why we are restricted to declare static member variable in inner Class in java? 即使可以在其他地方访问类,也可以使用java.lang.ClassNotFoundException - java.lang.ClassNotFoundException even though class can be accessed in other places 为什么我们要声明 ViewHolder 类是静态的? - Why should we declare the ViewHolder class static? 我们可以在Java中使用公共静态变量,在其他类中使用一个类的变量吗? - Can we use public static variable ,to use vaiable of one class in other class in java 为什么在 class 中的抽象 class 中有私有访问修饰符,即使我们不能创建抽象 class 的实例? - Why is there a private access modifier in an abstract class in Java, even though we cannot create an instance of an abstract class? 为什么我的 java 项目在缺少一些包的情况下仍然有效? - Why does my java project still work even though its missing some packages? 为什么不能在 Java 中将类声明为静态类? - Why are you not able to declare a class as static in Java? Java中的静态嵌套类,为什么? - Static nested class in Java, why? 为什么在定义类之前要声明嵌套类的变量 - Why can we declare a variable of a nested class before defining the class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM