Inner class is a class which is defined inside another class
in Java. Inner class is used to serve the following purposes in Java:
ü To increase
the logical grouping of classes based on some criteria.
ü To access
the private member of a class inside other class i.e. to increase
encapsulation.
ü To keep
related classes together i.e. to increase the maintenance.
ü To make the
code more readable and maintainable.
Types of inner class are:
i.
Static Inner class: A class
which is declared inside another class with ‘static’ modifier are called as
static inner class. It is static member for its outer class.
Such class can all the constructors and initialization blocks
as its parent class.
You can access static inner only by referencing its outer
class. For accessing the instance inner member of static inner class then you
have to create the instance of static inner class using the outer class and
then you can access instance inner member of static inner class.
You should access static inner class members using static
inner class name (for static member) and with the instance of static class (for
instance member). But in any case static inner class must be accessed using the
name of its outer class.
ii.
Instance Inner class: A class which is declared without ‘static’
modifier inside another class is called as instance inner class. It serves as
instance of its outer class.
To access the instance inner class
you must create the instance of outer class and using that instance of outer
class you can access instance inner class. In instance inner class only ‘static
final’ variables are allowed to declare.
Accessing Instance Inner class:
a) Inside
outer class instance method:
To
access the ‘static final’ variable of instance inner class you have to create
the instance of inner class.
To
access the instance member of instance inner class then you have to create
instance of inner class without using the outer class instance.
b) Outside
outer class: To access the ‘final static’ member of instance inner class
then you must provide names of inner class as well as outer class as:
Ex: Student.FeeDetails.totalfee;
Here
‘FeeDetails’ is Instance inner class of ‘Student’ class and ‘totalfee’ is final
static variable in ‘FeeDetails’.
For accessing instance member of Instance inner class:
ü First of
all, create instance of outer class.
ü Using this
instance of outer class create instance for inner class.
ü Using this
instance of inner class you can access instance member of inner class as:
Ex:
Student st=new Student();
Student.FeeDetails fee=st.new
FeeDetails();
iii.
Local Inner class:
Local inner class is class which is declared inside any
method/constructor/block. The scope of local inner class inside the block in
which it is declared and local inner class in accessible only in context in
which it is declared i.e. inside method/constructor/block in which it is
declared.
Only ‘final’ local variable of
method/constructor/block can be accessed inside the local inner class.
With local inner class declaration you can use only
two modifiers as: final or abstract.
You cannot create reference variable for local inner
class outside the, method/constructor/block in which it is declared.
iv.
Anonymous Inner class: Anonymous
is a class without any class name. It must be subclass of other and it can be
declared inside any other class or outside also. Anonymous class cannot members
with ‘static’ modifiers.
It can be declared between the constructor call and
semicolon.
Class file for the anonymous class will be generated
as:
‘intvalue’ is value assigned by compiler to identify
and manipulate the anonymous class internally and it depends on the textual
order of such anonymous classes in source file.
Anonymous class can have only one instance and the
reference of anonymous class should be stored in reference of its parent type
New members can be defined in anonymous class but
those members should not be declared in parent class already.
Anonymous class can also be created with ‘interface’
as its super type.
0 comments:
Post a Comment