5

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?

3
  • I can't confirm completely, but to the best of my knowledge, the answer to your question is: No, there are no other differences. Commented May 19, 2015 at 21:04
  • 1
    I might be wrong but I remember reading somewhere that non-static inner classes have an implicit reference to the outer class but static inner classes don't. Commented May 19, 2015 at 21:15
  • @Naveed, I will have to do some more research. However, I am pretty sure you are thinking of non-static methods having an implicit reference to the object from which they have been called, whereas static methods do not have said reference because they belong to the class and not any individual instance. In other words: You can use the "this" keyword inside a non-static method, but not in a static method. Commented Jul 25, 2016 at 6:36

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

I am choosing this answer as correct because all the additional "qualities" that the static nested class has, over and above some other top-level class, are conferred because it is within the scope of the enclosing class, even though it is not a member of said enclosing class.
A nested static class is indeed a member of the outer class: "A class body may contain declarations of members of the class, that is, fields (§8.3), methods (§8.4), classes (§8.5), and interfaces (§8.5)." docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.1.6 and "A member class is a class whose declaration is directly enclosed in the body of another class or interface declaration (§8.1.6, §9.1.4)." docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.5
2

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.

1 Comment

Good point. In this case it is a matter of who can access these classes instead of what can these classes access. I gotta remember to think in both directions.
2

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.

Comments

1

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.

1 Comment

Thanks. That's kind thing I'm looking for. That could certainly trip up a beginner.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.