Nested classes and inner classes

Nested class: 
Define a classes within another class is called nested classes. The scope of the nested class is within the class. If class B is defined within class A, then B is known to A, but not outside of A. A nested class has access to the members, including private members, of the class in which it is nested. Enclosing class does not have access to the members of the nested class.
There are two type of nested class: --------------
  • Static
  • Non-static   -----   inner class
A static nested class is one which has to define with static keyword or which has static modifier. Because of the static, it must access the members of its enclosing class through an object. it can’t refer to members of its enclosing class directly.
The most important types of the nested class is the inner class.

Inner Class
   

An inner class is a non-static nested class. It has access to all of the variables and methods of its outer class and may refer to them directly in the same way that other non-static methods of the outer class do. Thus, an inner class is fully within the scope of its enclosing class.

Example:
 
class Outer
{
int p=99;
void test()
{
Inner inner= new Inner();
inner.display();
}
class Inner
{
void display()
{
System.out.println("display:p="+p);
}
}
}

class T13
{
public static void main(String as[])
{
Outer outer=new Outer();
outer.test();
}
      }


OUTPUT:
display:p=99




Here inner class name Inner defined within the Outer class. Therefore ,any code in class Inner can directly access the variable p. An instance method named display() is defined inside Inner. the main method of  T13 creates an instance of the  class Outer and invokes its test() method. that method creates an instance of class
Inner and the display method is called.
the class Inner is known only within the scope of class Outer. if any code outside the class Outer java compiler gives Error message. An inner class has access to all of the members of its enclosing class, but the reverse is not  true. member of the inner class are known only within the scope of the inner class and  may not be used by the outer class.


class Outer
{
int x=88;
void test()
{
Inner inner=new Inner();
inner.display();
}
class Inner
{
int y=99;
void display()
{
System.out.println("display:x="+x);
System.out.println("y="+y);
}
}
void showy()
{
//System.out.println(y);  //error y not know here!
}
}


class T13
{
public static void main(String as[])
{
Outer outer=new Outer();
outer.test();
}
      }

OUTPUT:

display:x=88
y=99



Here y is declared as an instance variable of Inner. Thus it is not known outside of that class and it cannot be used by showy().
class Outer
{
int x=88;
void test()
{
for(int i=0;i<6;i++)
{
class Inner
{
int y=99;
void display()
{
System.out.println("display:x="+x);
System.out.println("y="+y);
}
}
Inner inner=new Inner();
inner.display();
}
}
}





class T14
{
public static void main(String as[])
{
Outer outer=new Outer();
outer.test();
}
}


OUTPUT
display:x=88
y=99
display:x=88
y=99
display:x=88
y=99
display:x=88
y=99
display:x=88
y=99
display:x=88
y=99



NOTE:
Anonymous inner class: ------which are inner classes that don't have a name.

0 comments :

Post a Comment