logo
Java Member Inner Classes
Member inner classes
1. If we are declaring any data in outer class then it is automatically available to inner classes.
2. If we are declaring any data in inner class then that data is should not have the scope of the outer class.
Syntax:-
class Outer
{
class Inner
{
};
};
Object creation syntax:-
Syntax 1:-
OuterClassName o=new OuterClassName();
OuterClassName.InnerClassName oi=OuterObjectreference.new InnterClassName();
Syntax 2:-
OuterclassName.InnerClassName oi=new OuterClass().new InnerClass();
Note:- by using outer class name it is possible to call only outer class peroperties and methods and by using inner class object we are able to call only inner classes properties and methods.
Member inner classes program
class Outer 
{ 
private int a=100; 
class Inner 
{ 
void data() 
{ 
System.out.println("the value is :"+a); 
} 
} 
} 
class Test 
{ 
public static void main(String[] args) 
{ 
Outer o=new Outer(); 
Outer.Inner i=o.new Inner(); 
i.data(); 
} 
};
Output :
the value is :100
this keyword in inner class
class Outer 
{ 
int a=100; 
class Inner 
{ 
int a=200; 
void m1(int a) 
{ 
System.out.println(a); 
System.out.println(this.a:); 
System.out.println(Outer.this.a); 
} 
}; 
}; 
class Test 
{ 
public static void main(String[] args) 
{ 
Outer o=new Outer(); 
Outer.Inner i=o.new Inner(); 
i.m1(300); 
} 
};
Output :
output:300
200
100