innerclasses
Inner Classes in java:
Inner classes are the nested classes.
example of a inner class:
class MyOuter{
class MyInner{
public void display(){
System.out.println("Inner class");
}
}
}
Type- 1:
aCCESS outer class variable from inner class
class MyOuter{
private int i=10;
class MyInner{
public void display(){
System.out.println("Accessing private member of the outer"+i);
}
}
}
Type-2:
Create an Instance of Inner class in outer class:
class MyOuter{
private int i=0;
public void makeInner(){
/*Creating an instance of the inner class in the outer class*/
MyInner in= new MyInner();
/*call the inner class method*/
in.display();
}
class MyInner{
public void display(){
system.out.println("Accessing private memeber of the outer"+i);
}
}
}
Type-3:
Create an Instance of Inner claSS from outside outerclass
class MyOuter{
private int i=15;
public void makeInner(){
/*Creating an instance of the inner class in the outer class*/
MyInner in= new MyInner();
/*call the inner class method*/
in.display();
}
class MyInner{
public void display(){
System.out.println("Accessing private memeber of the outer"+i);
}
}
public static void main(String args[]){
/*Create an instance of the outer class*/
MyOuter myouter= new MyOuter();
/*Use the outer class reference to create an instance of inner class*/
MyOuter.MyInner inner=myouter.new MyInner();
inner.display();
}
}
Type-4:
/**
*
* @author Anjana
*/
class MyOuter{
private int i=10;
class MyInner{
public void display(){
System.out.println("Printing the private variable of the Outer class"+i);
}
}
}
public class A {
public static void main(String args[]){
MyOuter myouter=new MyOuter();
MyOuter.MyInner myinner=myouter.new MyInner();
myinner.display();
System.out.println(myinner.getClass());
}
}
Type-5:
package com.tutorial.examples;
/**
*
* @author ADMIN
*/
class MyOuter{
private int i=15;
public void makeInner(){
/*Creating an instance of the inner class in the outer class*/
MyInner in= new MyInner();
/*call the inner class method*/
in.display();
}
class MyInner{
public void display(){
System.out.println("Accessing private memeber of the outer"+i);
}
public void seeOuter(){
System.out.println("Inner class ref is"+this);
System.out.println("Outer class ref is"+MyOuter.this);
}
}
public static void main(String args[]){
MyOuter myouter=new MyOuter();
MyOuter.MyInner myinner=myouter. new MyInner();
myinner.display();
myinner.seeOuter();
}
}
/**output/
Accessing private memeber of the outer15
Inner class ref iscom.tutorial.examples.MyOuter$MyInner@19821f
Outer class ref iscom.tutorial.examples.MyOuter@addbf1
BUILD SUCCESSFUL (total time: 0 seconds)
type-6
/*method local inner class*/
Define an inner class within a method:
Inner classes are the nested classes.
example of a inner class:
class MyOuter{
class MyInner{
public void display(){
System.out.println("Inner class");
}
}
}
Type- 1:
aCCESS outer class variable from inner class
class MyOuter{
private int i=10;
class MyInner{
public void display(){
System.out.println("Accessing private member of the outer"+i);
}
}
}
Type-2:
Create an Instance of Inner class in outer class:
class MyOuter{
private int i=0;
public void makeInner(){
/*Creating an instance of the inner class in the outer class*/
MyInner in= new MyInner();
/*call the inner class method*/
in.display();
}
class MyInner{
public void display(){
system.out.println("Accessing private memeber of the outer"+i);
}
}
}
Type-3:
Create an Instance of Inner claSS from outside outerclass
class MyOuter{
private int i=15;
public void makeInner(){
/*Creating an instance of the inner class in the outer class*/
MyInner in= new MyInner();
/*call the inner class method*/
in.display();
}
class MyInner{
public void display(){
System.out.println("Accessing private memeber of the outer"+i);
}
}
public static void main(String args[]){
/*Create an instance of the outer class*/
MyOuter myouter= new MyOuter();
/*Use the outer class reference to create an instance of inner class*/
MyOuter.MyInner inner=myouter.new MyInner();
inner.display();
}
}
Type-4:
/**
*
* @author Anjana
*/
class MyOuter{
private int i=10;
class MyInner{
public void display(){
System.out.println("Printing the private variable of the Outer class"+i);
}
}
}
public class A {
public static void main(String args[]){
MyOuter myouter=new MyOuter();
MyOuter.MyInner myinner=myouter.new MyInner();
myinner.display();
System.out.println(myinner.getClass());
}
}
Type-5:
package com.tutorial.examples;
/**
*
* @author ADMIN
*/
class MyOuter{
private int i=15;
public void makeInner(){
/*Creating an instance of the inner class in the outer class*/
MyInner in= new MyInner();
/*call the inner class method*/
in.display();
}
class MyInner{
public void display(){
System.out.println("Accessing private memeber of the outer"+i);
}
public void seeOuter(){
System.out.println("Inner class ref is"+this);
System.out.println("Outer class ref is"+MyOuter.this);
}
}
public static void main(String args[]){
MyOuter myouter=new MyOuter();
MyOuter.MyInner myinner=myouter. new MyInner();
myinner.display();
myinner.seeOuter();
}
}
/**output/
Accessing private memeber of the outer15
Inner class ref iscom.tutorial.examples.MyOuter$MyInner@19821f
Outer class ref iscom.tutorial.examples.MyOuter@addbf1
BUILD SUCCESSFUL (total time: 0 seconds)
type-6
/*method local inner class*/
Define an inner class within a method:
Comments
Post a Comment