What is a top level class in java?


  • What is a top level class in java?
  • What is the definition of a top level class in java ?

I know that this is a basic question, but i could not find a clear and simple answer for this question.


Answers:


It's simply any class that's not a nested class. If your file is Foo.java, and your class is Foo, then it's a top level class.

// in Foo.java:

public class Foo { // top level class

    public static class NestedBar { // nested class
    }

}

I had always thought this was an informal term, but it turns out it's actually defined in the JLS:

A top level class is a class that is not a nested class.

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