MY mENU


Saturday 11 February 2012

Inner Class in Java


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:
  1. public class User {  
  2.   
  3.     //  User fields, including variables of type LoginInfo and UserPreferences  
  4.     // Misc user methods  
  5.   
  6.     class LoginInfo  
  7.     {  
  8.         // Login info fields  
  9.         // Login/Logout methods  
  10.         // Can access User fields/methods  
  11.     }  
  12.   
  13.     class Preferences  
  14.     {  
  15.         // User preference fields  
  16.         // Get/Set preference methods  
  17.         // Reset preferences method  
  18.         // Can access User fields/methods  
  19.     }  
  20. }  
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:
  1. public class User {  
  2.   
  3.     //  User fields, including variables of type LoginInfo and UserPreferences  
  4.     // Misc user methods  
  5.   
  6.     class LoginInfo {}  
  7.   
  8.     public static class ServerInfo {}  
  9.     {  
  10.         // Server info applies to all instances of User  
  11.     }  
  12. }  
Because it’s public, this static nested class can be instantiated using the following new statement:
  1. 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