Java – should the constructor of a private inner class be declared public or private?
•
Java
Is there any practical difference
public class OuterClass {
private class InnerClass {
public InnerClass() {}
}
}
And this?
public class OuterClass {
private class InnerClass {
private InnerClass() {}
}
}
Solution
Accessing private members from another class is a little more complicated because the JVM does not actually allow this As a result, the compiler injects accessor methods to make them slower or more complex stack traces
For these reasons, I regard it as a local package
The constructor of btw abstract class does not need to be public It can also be protected or packaged locally
private static class A {
private A() {
throw new Error();
}
}
public static void main(String... ignored) {
new A();
}
Prints an additional stack trace element
Exception in thread "main" java.lang.Error
at Main$A.<init>(Main.java:8)
at Main$A.<init>(Main.java:6)
at Main.main(Main.java:12)
Localize the constructor package, the second message
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
