Home >Java >javaTutorial >Static vs. Inner Classes in Java: What's the Difference?
In Java, the concept of nested classes is a powerful tool for organizing and structuring code. However, it can be confusing to understand the differences between static and non-static (inner) classes. This article aims to clarify these concepts and shed light on their distinct characteristics.
A nested class, also known as an inner class, is a class that is defined within another class. By nature, nested classes cannot be declared as static. They have full access to all members of the enclosing class, including private ones, and can invoke non-static methods and access non-static fields of an instance of the enclosing class.
A static nested class, on the other hand, is a class that is declared as static within an enclosing class. Unlike non-static nested classes, static nested classes do not have a reference to a nesting instance. This means that they cannot invoke non-static methods or access non-static fields of an instance of the enclosing class. Static nested classes have access only to the static members of the enclosing class.
The above is the detailed content of Static vs. Inner Classes in Java: What's the Difference?. For more information, please follow other related articles on the PHP Chinese website!