Search code examples
javaclassjls

Are nested classes and member classes the same thing?


Can the Java terms nested class and member class be used interchangeably or not?

From the JLS:

A nested class is any class whose declaration occurs within the body of another class or interface.

[…]

A member class is a class whose declaration is directly enclosed in the body of another class or interface declaration

I'm thinking the term member classes might not include anonymous classes and local classes, but this is just guesswork on my part.


Solution

  • Here's a good quote from Chapter 8. Classes:

    Member class declarations describe nested classes that are members of the surrounding class. Member classes may be static, in which case they have no access to the instance variables of the surrounding class; or they may be inner classes.

    As a bonus quote, local classes are definitely not member classes:

    A local class is a nested class that is not a member of any class [...].


    class Foo {
        // a member class
        class InstanceMember {}
        // a member class
        static class StaticMember {}
    
        Foo() {
            // not a member class
            class LocalAndNotMember {}
            // not a member class
            Object anonymousAndNotMember = new Object() {};
        }
    }