Java Nested Class





Sometimes, though, you find yourself designing a class where you discover you need behavior, that belongs in a separate, specialized class, but also needs to be intimately tied to the class you’re designing.

  • Inner classes let you define one class within another.

  • They provide a type of scoping for your classes since you can make one class a member of another class.

  • Just as classes have member variables and methods, a class can also have inner classes

Type of Inner Class

1. Nested Class (Static Inner Class)

2. Inner Class (Non-Static Inner Class)

Nested Class (Static Inner Class):

  • A nested class marked as static.
  • Requires an object to access the instance variable of its outer class.
  • Outer class also requires ab object to access the instance variable of its nested class.
class Outer
{
  int i =1;
  static int j = 2;
  static class Nested
   {
     int x = 3;
     static int y = 4;
    void display(){
     Outer ob = new Outer();
     System.out.println("i "+ob.i);
     System.out.println("j "+j);
     System.out.println("x "+x);
     System.out.println("y "+y);
    }
    static void get(){
    System.out.println("Static Method");
    }
   }
  void show()
  {
   Nested ob = new Nested();
   System.out.println("i "+i);
   System.out.println("j "+j);
   System.out.println("x "+ob.x);
   System.out.println("y "+ob.y);
  }

}
class MemberClassDemo {
public static void main(String []ar){
Outer.Nested.get();
Outer.Nested n = new Outer.Nested();
n.display();
}
}            
OUTPUT :
Static Method
i 1
j 2
x 3
y 4 

Inner Class (Non-Static Inner Class)

Types

1. Regular Inner Class

2. Method Local Inner Class

3. Anonymous Inner Class


Regular Inner Class

  • One of the key benefits of an inner class is the "special relationship" an inner class instance shares with an instance of the outer class.

  • That "special relationship" gives code in the inner class access to members of the enclosing (outer) class.

  • An inner class instance has access to all members of the outer class, even those marked private.

  • The members of inner of class cannot be declared as static.

class Outer {
void show() {
System.out.println("Show of Outer Class.");
}
class Inner {
void disp() {
System.out.println("Disp of Inner Class.");
}
}//End Of Inner Class
}//End Of Outer Class
class MemberClassDemo {
public static void main(String [] ar){
Outer o = new Outer();
o.show();
Outer.Inner i = o.new Inner();
//or
Outer.Inner i1 = new Outer().new Inner();
i.display();
}
}              
OUTPUT :
Show of Outer Class.
Display of Inner Class.

Method Local Inner Class

  • We can define class within a method that type of classes are called method local inner class.

  • A method-local inner class can be instantiated only within the method where the inner class is defined.

  • We can’t mark a method-local inner class as public, private, protected, static, transient . The only modifiers you can apply to a method-local inner class are abstract and final.

class Test {
   private int a=3;
   public void mX()
   {
       int p = 4;
       int q = 20;
       class MethodLocalClass{
           void mY()
           {
               System.out.println(a);
               System.out.println(p);
           }
       }
       new MethodLocalClass().mY();
   }
}
class MemberClassDemo {
public static void main(String []ar)
{
 Test ob = new Test();
 ob.mX();
}
}           
OUTPUT :
3
4           

Anonymous Inner Class :

  • An anonymous inner class is one that is not assigned a name.

  • We're going to look at the most unusual syntax you might ever see in Java, inner classes declared without any class name at all (hence the word anonymous).

  • Java Anonymous inner class can be created by two ways:

  • 1. Class (may be abstract or concrete).

  • 2. Interface.

class Test {
void show() {
System.out.println("Hello from Test");
}
}//End Of Test Class
class AICDemo {
public static void main(String []ar){
Test t1 = new Test();
//Anonymous Inner Class
Test t2 = new Test(){
void show() {
System.out.println("Anonymous Show");
}
}; //End Of Anonymous Inner Class
t1.show();
t2.show();
}
}          
OUTPUT :
Hello from Test
Anonymous Show

Anonymous Implementer :

interface X{
    void show();
}
class Test{
  X ref = new X(){

  @Override
  public void show(){
    System.out.println("Hello");  
    }
  };
public static void main(String[] ar){
  Test t=new Test();
  t.ref.show();
 }
}
OUTPUT :
Hello

Argument-Defined Anonymous Inner Class :

interface A{
    void disp();
}
interface B{
    void show(A ref);
}
class Test{
   B ref = new B() {

   @Override
   public void show(A ref) {
    System.out.println("Hello");
     ref.disp();
    }
   };
 public static void main(String[] ar){
   Test t = new Test();
   t.ref.show(new A() {

    @Override
      public void disp() {
       System.out.println("Hi");
       }
     });
    }
}
OUTPUT :
Hello
Hi

Next topic is enum




 









Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in



CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved