Reputation: 479
I know this is a bit of a duplicate question but I want to ask it in a very specific way in order to clarify a very important point. The primary question being: Is there any difference at all between otherwise identical classes when one is a static nested class and the other is a regular, top-level, class other than access to private static fields in a containing class?
// ContainingClass.java
public class ContainingClass {
private static String privateStaticField = "";
static class ContainedStaticClass {
public static void main(String[] args) {
ContainingClass.privateStaticField = "new value";
}
}
}
// OutsideClass.java
public class OutsideClass {
public static void main(String[] args) {
ContainingClass.privateStaticField = "new value"; // DOES NOT COMPILE!!
}
}
In other words: Is the only, ONLY difference, between what ContainedStaticClass
can access or do and what OutsideClass
can access or do, the fact that OutsideClass
cannot access ContainingClass.privateStaticField
directly? Or are there other, subtle differences that aren't commonly discussed or ran into?
Upvotes: 5
Views: 510
Reputation: 51037
There are several subtle differences.
Probably the most important one is that a nested static class can be made private
or protected
. A top-level class can only be package-private or public
.
The other differences I can think of are technical details, which you probably should not write code where those details matter:
A nested static class can "hide" or "shadow" other classes of the same name. When a nested static class has the same name as an outer class, and both are in scope, the nested static class hides the outer class.
A nested static class is inherited by subclasses of its outer class, so it is in scope in those subclasses and can be referred to without a fully-qualified name or a static import.
Two outer-level classes in the same package cannot share the same name, but a class can inherit multiple nested static classes of the same name (e.g. from two interfaces). This does not result in a compilation error unless the nested class's name is used ambiguously.
A nested static class can have multiple different fully-qualified names due to inheritance, for example in class A { static class B extends A {} }
the class A.B
can be referred to as A.B.B
, A.B.B.B
, and so on. An outer class can only have one fully-qualified name.
An outer class's canonical name equals its binary name, but a nested static class's canonical name is not equal to its binary name. The binary name of a nested static class has the symbol $
separating the outer class's binary name from the nested static class's simple name.
Upvotes: 2
Reputation: 718728
Another subtle difference occurs in reflective use-cases. The class name you need to load a class with Class.forName
is a bit funky.
Class.forName("com.acme.OuterClass.Innerclass"); // not found
Class.forName("com.acme.OuterClass$Innerclass"); // correct
See also: Instantiate nested static class using Class.forName.
Upvotes: 0
Reputation: 27946
Your statement is correct: the only difference between a static class and and a outer class is access to the class and the members of the enclosing class. The static
keyword is declaring that the class is not an inner class: it is in effect an outer class within the scope of the enclosing class.
See https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.5.1
Upvotes: 2
Reputation: 10891
ContainedStaticClass has package private (i.e. default) visibility and OutsideClass has public visibility.
You could have chosen to make ContainedStaticClass protected or private which were not options for OutsideClass.
Upvotes: 2
Reputation: 72844
A subtle difference is that a nested class has the ability to shadow members of the enclosing type:
class ContainingClass {
public static String privateStaticField = "a";
static class ContainedStaticClass {
public static String privateStaticField = "b";
public static void main(String[] args) {
System.out.println(privateStaticField); // prints b
}
}
}
But that's also related to the scope of the class.
Upvotes: 1