What is Instance Inner class?

Instance Inner class:--

                         The class which is defined inside the another class without static keyword is called instance inner class.



package com.javaforecast4u;

/**
 * @author Bikash
 *
 */
public class Outer {

int a=10;
static int b=20;
void m1(){
System.out.println("Outer start");
System.out.println(a);
System.out.println(b);

Inner i=new Inner();
System.out.println(i.x); //2..
i.show();
i.test();
System.out.println("Outer end");
}

Inner i=new Inner();

class Inner{
int x=100;
//static y=200;  //3.
void show(){
System.out.println("Inner start");
System.out.println(a);  //1.
System.out.println(b);  //1.
System.out.println(x);

}

void test(){
System.out.println("test()");
}
}

}


package com.javaforecast4u;

/**
 * @author Bikash
 *
 */
public class InstanceInner {


public static void main(String[] args) {
Outer o=new Outer();
o.m1();

Outer.Inner oi1=o.new Inner();
oi1.show();

Outer.Inner oi=new Outer().new Inner();
oi.test();



}

}

Output:----
Outer start
10
20
100
Inner start
10
20
100
test()
Outer end
Inner start
10
20
100
test()

Points:---

1.Outer class members can be access inside the inner class directly.
2. but Inner class member can't be access inside the Outer class directly.
    access with an object.
     Inner i=new Inner();
System.out.println(i.x);
3.Instance Inner class can't contain static declaration.
4.To create object of Inner class outside the Outer class ,we use one of the following special syntax.

  i)  Outer o=new Outer();
      Outer.Inner oi=o.new Inner();

 ii)  Outer.Inner oi1=new Outer().new Inner(); 



0 comments :

Post a Comment