What is a top level class in java?

Harsha Jayamanna :
  • 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.

yshavit :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=422106&siteId=1