Search This Blog

Monday 22 October 2012

Advance Java : Inner Classes Concept in Java

Inner classes was introduces in JDK 1.1. And Inner class refer to a class appear with in

• class declarations
• method bodies
• expressions

Reason To Use Nested Classes From docs.oracle.com

Logical grouping of classes—If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.

Increased encapsulation—Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.

More readable, maintainable code—Nesting small classes within top-level classes places the code closer to where it is used.

Scope of an Inner class differ according to which type of Inner class you are using. We have four type in Inner Class

  •   Member Inner (MI) Classes
  •   Local Inner (LI) Classes
  •   Anonymous Inner (AI) Classes
  •   Static Inner (SI) Classes


  Let see one Example of Member Inner Class

public class A {

 public static void main(String[] args) {
  /**
   * Accessing Member Inner Classes
   */
  MemberInnerClass member = new A().new MemberInnerClass();
  member.printMessage();
 }

 public class MemberInnerClass {

  public void printMessage() {
   System.out.print("I am inside Member Inner Claas");
  }
 }
} 
  
 To accessing MI you have to create Object of Parent class(outer class). You can make MI Read Only by creating on private constructor

Local Inner Class is created inside a Method and its scope remains same method Member Variables


public class A {

 public static void main(String[] args) {
  /**
   * Accessing Local Inner Classes
   */
  new A().LocalLimit("I am inside Local Inner Class");
 }

 public void LocalLimit(final String message) {
  
  class LocalInnerCalss {
   public void printMessage() {
    System.out.print(message);
   }
  }
  new LocalInnerCalss().printMessage();
 }
}

Static Inner Class-- Its useful when modeling tightly coupled entities.Static class also provide Providing meaningful namespaces


public class StaticInner {
 public static void main(String[] args) {
  /**
   * Accessing Static Inner Classes
   */
  StaticInner.MemberInnerClass member = new StaticInner.MemberInnerClass();
  member.printMessage();
 }

 public static class MemberInnerClass {

  public void printMessage() {
   System.out.print("I am inside Static Inner Claas");
  }
 }
}

About Anonymous Inner Class, I will explain in a separate article as its need more explanation.

Now At the End some Use full tips to remember

Remember while declaring an inner class
• MI and SI appear within the body of a class
• LI and AI appear within the body of a method

Remember while accessing outer fields
• Both MI, LI and AI can refer to fields of their enclosing scope
• A SI can’t, since it doesn’t have an enclosing instance!

Article You May Like

Synchronization Of thread in Java, Anonymous Inner Class Explanation

No comments:

Post a Comment

Feedback always help in improvement. If you have any query suggestion feel free to comment and Keep visiting my blog to encourage me to blogging

Android News and source code