Most classes in Java are top-level classes. These classes, and the objects they define, are stand-alone. You can also create nested classes in order to clearly encapsulate and define subordinate objects that only matter in the context of the outer class. Nested classes are called inner classes.
Inner classes can have all the features of a regular class, but their scope is limited. Inner classes have another benefit: they have full access to the class in which they are nested—this feature makes inner classes perfect for implementing adapter functionality like iterators.
Here’s an example of a top-level class with two inner classes:
- public class User {
- // User fields, including variables of type LoginInfo and UserPreferences
- // Misc user methods
- class LoginInfo
- {
- // Login info fields
- // Login/Logout methods
- // Can access User fields/methods
- }
- class Preferences
- {
- // User preference fields
- // Get/Set preference methods
- // Reset preferences method
- // Can access User fields/methods
- }
- }
In this example, the User class has two inner classes: LoginInfo and Preferences. While all user-related data and functionality could be defined in the User class, using the inner classes to compartmentalize functionality can make code easier to read and maintain. The inner classes LoginInfo and Preferences also have access to the protected/private fields and methods available within the User class, which they might not otherwise have due to security, if they were defined as stand-alone classes themselves.
It’s important to remember, though, that inner classes really only exist to help the developer organize code; the compiler treats inner classes just like any other class, except that the inner classes have a limited scope, and are therefore tethered to the class they are defined with. Said another way, you would not be able to use or instantiate the LoginInfo or Preferences classes except with an instance of the User class, but the inner classes could access any fields or methods available in the outer class User, as needed.
Using Static Nested Classes:
One particularly use for nested classes is static nested classes. A static inner class defines behavior that is not tied to a specific object instance, but applies across all instances. For example, we could add a third nested class, this time static, to the User class to control server-related functionality:
- public class User {
- // User fields, including variables of type LoginInfo and UserPreferences
- // Misc user methods
- class LoginInfo {}
- public static class ServerInfo {}
- {
- // Server info applies to all instances of User
- }
- }
Because it’s public, this static nested class can be instantiated using the following new statement:
- User.ServerInfo sInfo = new User.ServerInfo();
The outer class does not have to be instantiated to perform this instantiation, thus the use of the class name. As such, a static nested class, unlike a non-static nested class (aka inner class) does not have access to members of the outer class–they may not even be instantiated.
No comments:
Post a Comment