0% found this document useful (0 votes)
11 views6 pages

Understanding Java Inner Classes

Java inner classes, or nested classes, are defined within another class or interface, allowing for better organization and access to outer class members. There are two main types of nested classes: non-static (inner classes) and static nested classes, each with specific use cases and advantages. Inner classes enhance code readability, maintainability, and optimization by logically grouping related classes and enabling access to private members of the outer class.

Uploaded by

Get Lost
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

Understanding Java Inner Classes

Java inner classes, or nested classes, are defined within another class or interface, allowing for better organization and access to outer class members. There are two main types of nested classes: non-static (inner classes) and static nested classes, each with specific use cases and advantages. Inner classes enhance code readability, maintainability, and optimization by logically grouping related classes and enabling access to private members of the outer class.

Uploaded by

Get Lost
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Java Inner Class

Java inner class or nested class is a class i.e. 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.

Syntax

class Java_Outer_class{
//code

class Java_Inner_class{

//code

}
Advantage of java inner 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.

Difference between nested class and inner class in Java


Inner class is a part of nested class. Non-static nested classes are known as inner classes.

Types of Nested classes


There are two types of nested classes; non-static and static nested [Link] non-static nested classes are
also known as inner classes.

1. Non-static nested class(inner class)

a)Member inner class

b)Anonymous inner class

c)Local inner class

2. Static nested class

1
Java Member inner class
A non-static class that is created inside a class but outside a method is called member inner class.

Syntax:

class Outer{

//code

class Inner{

//code

} }

Example:

classTestMemberOuter{

private int data=30;

class Inner{

void msg(){[Link]("data is "+data);}

public static void main(String args[])

TestMemberOuter obj=new TestMemberOuter();

[Link] in=[Link] Inner();

[Link]();

2
}

Note:The java compiler creates two class files in case of inner class. The class file name of inner
class is "TestMemberOuter$Inner".

Java Anonymous inner class


A class that has no name is known as anonymous inner class in java.

It should be used if you have to override method of class or interface. Java Anonymous inner class can be
created by two ways:

1. Class (may be abstract or concrete).


2. Interface

Java anonymous inner class example using class


abstract class Person{

abstract void eat();

classTestAnonymousInner{

public static void main(String args[]){

Person p=new Person(){

void eat()

[Link]("nice fruits");

};

[Link]();

1. 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.
2. An object of Anonymous class is created that is referred by “p” reference variable of Person type.

3
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(){[Link]("nice fruits");}

};

[Link]();

Java Local inner class


A class i.e. created inside a method is called local inner class in java. If you want to invoke the methods
of local inner class, you must instantiate this class inside the method.

Example

public class TestLocalInner{

privateint data=30;//instance variable

void display(){

class Local{

voidmsg(){[Link](data);}

Local l=new Local();

[Link]();

public static void main(String args[]){

4
TestLocalInnerobj=new TestLocalInner();

[Link]();

} }

Note: Local inner class cannot be invoked from outside the method.

Java static nested class


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.

Example

classTestOuter{

staticint data=30;

static class Inner{

voidmsg(){[Link]("data is "+data);}

public static void main(String args[]){

[Link]=new [Link]();

[Link]();

Java static nested class example with static method


If you have the static member inside static nested class, you don't need to create instance of static nested
class.

classTestOuter{

staticint data=30;

static class Inner{

static void msg(){[Link]("data is "+data);}

public static void main(String args[]){

5
[Link]();//no need to create the instance of static nested class

You might also like