- Java inner classes are classes declared within other classes or interfaces. They allow grouping of logically related classes and interfaces and can access all members of the outer class, including private ones.
- There are three main advantages of inner classes: they can access private members of the outer class, they make code more readable by grouping related classes, and they require less code.
- The two types of inner classes are non-static (inner) classes and static nested classes. Non-static classes can access outer class members like private variables while static classes cannot access non-static members only static ones.
- Examples demonstrate member inner classes, anonymous inner classes, local inner classes, and static nested classes in Java and how they can
Interface in Java
Java inner class or nested class is a class
which is declared inside the class or
interface.
We use inner classes to logically group
classes and interfaces in one place so that
it can be more readable and maintainable.
Additionally, it can access all the members
of outer class including private data
members and methods.
3.
Syntax of Innerclass
class Java_Outer_class{
//code
class Java_Inner_class{
//code
}
}
4.
Advantage of javainner classes
There are basically three advantages of inner classes in java. They
are as follows:
1) Nested classes represent a special type of relationship that is it
can access all the members (data members and methods) of
outer class including private.
2) Nested classes are used to develop more readable and
maintainable code because it logically group classes and
interfaces in one place only.
3) Code Optimization: It requires less code to write.
5.
Types of Nestedclasses
There are two types of nested classes
non-static and static nested classes.
The non-static nested classes are also known as
inner classes.
>Non-static nested class (inner class)
Member inner class
Anonymous inner class
Local inner class
>Static nested class
Java Member innerclass In this example, we are creating msg()
method in member inner class that is accessing the private data
member of 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();
}
}
8.
Java anonymous innerclass example using 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();
}
}
9.
Java local innerclass example
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();
}
}
10.
Java static nestedclass
A static class i.e. created inside a class is called 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 outer class including
private.
◦ Static nested class cannot access non-static (instance) data
member or method.
11.
Java static nestedclass example with
instance method
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();
}
}