I know that this is a basic question, but i could not find a clear and simple answer for this question.
The Object class is the parent class of all the classes in java by default. In other words, it is the topmost class of java.
No, we cannot declare a top-level class as private or protected. It can be either public or default (no modifier). If it does not have a modifier, it is supposed to have a default access.
Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control: At the top level— public , or package-private (no explicit modifier). At the member level— public , private , protected , or package-private (no explicit modifier).
We can't declare outer (top level) class as static because the static keyword is meant for providing memory and executing logic without creating Objects, a class does not have a value logic directly, so the static keyword is not allowed for outer class.
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.
The definition posted is correct, maybe another definition could be: a top-level class is the defined class with the same name of the file '.java' where it's in. however, just as an observation, for the case showed, a nested (not a top-level) 'static' class has the same behavior of a top-level class, in terms of the interaction with the instance members of the class where it has been nested.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With