Java Nested Classes (Inner Classes)
Java Nested Classes (Inner Classes)
CLASSES(INNER CLASSES )
nested class is a class that is declared inside the class or
interface.
We use inner classes to logically group classes and interfaces in
one place to be more readable and maintainable.
It can access all the members of the outer class, including
private data members and methods.
INTRO.
class Java_Outer_class{
//code
class Java_Inner_class{
//code
}
}
NEED
An inner class is a part of a nested class.
Non-static nested classes are known as inner classes.
SYNTAX
class Outer{
private int data=30;
class Inner{
void msg(){ System.out.println("data is "+data); }
}
public static void main(String args[]){
Outer obj=new Outer();
Outer.Inner in=obj.new Inner();
in.msg();
} }
EXAMPLE
An object or instance of a member's inner class always exists within an
object of its outer class.
The new operator is used to create the object of member inner class
with slightly different syntax.
SYNTAX: OuterClassRef.new InnerClassConstructor();
Example: obj.new Inner();
INTERNAL CODE
The java compiler creates two class files in the case of the inner
class.
The Java compiler creates a class file named Outer$Inner in this
case.
We must have to create the instance of the outer class.
The inner class has the reference of Outer class that is why it can
access all the data members of Outer class including private
INTERNAL CODE
import java.io.PrintStream;
class Outer$Inner
{
final Outer this$0;
Outer$Inner()
{ super();
this$0 = Outer.this;
}
void msg()
{
System.out.println((new StringBuilder()).append("data is ")
.append(Outer.access$000(Outer.this)).toString());
}
}
EXAMPLE
A class is created, but its name is decided by the compiler, which
extends the Person class and provides the implementation of the
eat() method.
An object of the Anonymous class is created that is referred to by
'p,' a reference variable of Person type.
import java.io.PrintStream;
static class Test$1 extends Person
{
TestAnonymousInner$1(){}
void eat()
{
System.out.println("nice fruits");
}
}
INTERNAL CODE
interface Eatable{
void eat();
}
class Test1{
public static void main(String args[]){
Eatable e=new Eatable(){
public void eat(){System.out.println("nice fruits");}
};
e.eat();
}
}
USING AN INTERFACE
A class i.e., created inside a method, is called local inner class in
java.
Created inside a block.
Sometimes this block can be a for loop, or an if clause.
Local Inner classes are not a member of any enclosing classes.
Local inner classes cannot have any access modifiers associated
with them.
They can be marked as final or abstract.
These classes have access to the fields of the class enclosing it.
EXAMPLE
import java.io.PrintStream;
class local_o$Local
{
final local_o this$0;
local_o$Local()
{
super();
this$0 = Simple.this;
}
void msg()
{
System.out.println(localInner1.access$000(local_o.this));
}
}
INTERNAL CODE
1) Local inner class cannot be invoked from outside the method.
EXAMPLE
A static class is a class that is created inside a class, is called a
static nested class in Java.
It cannot access non-static data members and methods.
It can be accessed by outer class name.
It can access static data members of the outer class, including
private.
The static nested class cannot access non-static (instance) data
members
EXAMPLE
public class Outer2{
static int data=30;
static class Inner{
static void msg(){System.out.println("data is "+data);}
}
public static void main(String args[]){
Outer2.Inner.msg();
//no need to create the instance of static nested class
}
}
EXAMPLE
import java.io.PrintStream;
static class TestOuter1$Inner
{
TestOuter1$Inner(){}
void msg(){
System.out.println((new StringBuilder()).append("data is ")
.append(TestOuter1.data).toString());
}
}
INTERNAL CODE