0% found this document useful (0 votes)
22 views4 pages

CJ Inner Classes

The document discusses different types of inner classes in Java including member inner classes, anonymous inner classes, local inner classes, and static nested classes. It provides examples and explanations of each type of inner class and how they can access members of outer classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

CJ Inner Classes

The document discusses different types of inner classes in Java including member inner classes, anonymous inner classes, local inner classes, and static nested classes. It provides examples and explanations of each type of inner class and how they can access members of outer classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Java Inner Classes (Nested Classes)

Java inner class or 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.
Additionally, it can access all the members of the outer class, including private data members and
methods.
Syntax of Inner class
class Java_Outer_class{
//code
class Java_Inner_class{
//code
}
}
Advantage of Java inner classes:
Nested classes represent a particular type of relationship that is it can access all the members (data
members and methods) of the outer class, including private.
Nested classes are used to develop more readable and maintainable code because it logically group
classes and interfaces in one place only.
Code Optimization: It requires less code to write.
Java Member Inner class
A non-static class that is created inside a class but outside a method is called member inner class. It is
also known as a regular inner class. It can be declared with access modifiers like public, default,
private, and protected.
Java Member Inner Class Example
In this example, we are creating a msg() method in the member inner class that is accessing the
private data member of the outer class.
class TestMemberOuter1{
private int data=30;
class Inner{
void msg(){System.out.println("data is "+data);}
}
public static void main(String args[]){
TestMemberOuter1 obj=new TestMemberOuter1();
TestMemberOuter1.Inner in=obj.new Inner();
in.msg();
}
}
Output: data is 30
The java compiler creates two class files in the case of the inner class. The class file name of the inner
class is "Outer$Inner". If you want to instantiate the inner class, you must have to create the
instance of the outer class. In such a case, an instance of inner class is created inside the instance of
the outer class.

Java Anonymous inner class

Java anonymous inner class is an inner class without a name and for which only a single object is
created. An anonymous inner class can be useful when making an instance of an object with certain
"extras" such as overloading methods of a class or interface, without having to actually subclass a
class.

abstract class Person{


abstract void eat();
}
class TestAnonymousInner{
public static void main(String args[]){
Person p=new Person(){
void eat(){System.out.println("nice fruits");}
};
p.eat();
}
}
Output: nice fruits
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.
Java anonymous inner class example using interface
interface Eatable{
void eat();
}
class TestAnnonymousInner1{
public static void main(String args[]){
Eatable e=new Eatable(){
public void eat(){System.out.println("nice fruits");}
};
e.eat();
}
}
A class is created, but its name is decided by the compiler, which implements the Eatable interface
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 the
Eatable type.
Java Local inner class
A class i.e., created inside a method, is called local inner class in java. Local Inner Classes are the
inner classes that are defined inside a block. Generally, this block is a method body. Sometimes this
block can be a for loop, or an if clause. Local Inner classes are not a member of any enclosing classes.
They belong to the block they are defined within, due to which local inner classes cannot have any
access modifiers associated with them. However, they can be marked as final or abstract. These
classes have access to the fields of the class enclosing it.
If you want to invoke the methods of the local inner class, you must instantiate this class inside the
method.
public class localInner1{
private int data=30;//instance variable
void display(){
class Local{
void msg(){System.out.println(data);}
}
Local l=new Local();
l.msg();
}
public static void main(String args[]){
localInner1 obj=new localInner1();
obj.display();
}
}

Java static nested class


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
class TestOuter1{
static int data=30;
static class Inner{
void msg(){System.out.println("data is "+data);}
}
public static void main(String args[]){
TestOuter1.Inner obj=new TestOuter1.Inner();
obj.msg();
}
}
In this example, you need to create the instance of static nested class because it has instance method
msg(). But you don't need to create the object of the Outer class because the nested class is static and
static properties, methods, or classes can be accessed without an object.
Java static nested class example with a static method
If you have the static member inside the static nested class, you don't need to create an instance
of the static nested class.
public class TestOuter2{
static int data=30;
static class Inner{
static void msg(){System.out.println("data is "+data);}
}
public static void main(String args[]){
TestOuter2.Inner.msg();//no need to create the instance of static nested class
}
}

You might also like