Nested Classes :
One
of the advantageous features of Java is the ability to define nested classes,
i.e. classes within classes. In java classes are either top-level-classes
(classed defined directly within a package) or nested classes (classes defined
within top-level-classes). We use nested classes to logically group classes at
one place so that it can be more readable and maintainable. It can also access
private data members of the class.
Syntax :
Class Outer_class{
Class
Nested_class{
//
Body of nested class
}
}
What are the advantages of using nested classes :
- Imagine the case in which a certain class is
being used only once from within another class, then there is no obvious need
to add a clutter to the enclosing package by defining a top-level-class that
will merely be used by only one another class.
- Another very important advantage of having
nested classes is the fact that nested classes will be able to access all the
class members of their enclosing top-level-class (including the private member
methods and variables).
-
Another advantage of this feature can be seen
when developing event-based applications where action and event listeners can
be defined as nested classes rather than having them as separate individual
top-level-classes.
- Code optimization as we need less code to write.
Types of Nested classes:
Non- Static nested class :
Non
static nested classes are defined within the top-level-classes and they are
bound by the scope of their enclosing class, non-static nested classes can have
direct access into all of the members (even the private ones) of their
enclosing class, on the other hand, the enclosing class doesn’t have any access
to the members of the enclosed nested class.
·
Member Class :
A class that is declared inside a
class but outside the method of the class. There are possible two ways through
which we can access inner class are – from within a class and from outside the
class.
Example : (from
within the class)
class OuterClass{
private int x = 20;
public class InnerClass{
public void show(){
System.out.println("x = "+x);
}
}
public void display(){
InnerClass inner = new InnerClass();
inner.show();
}
public static void main(String[]
args){
OuterClass o = new OuterClass();
o.display();
}
}
Example : (from
outside the class)
class OuterClass{
private int x =
20;
public class
InnerClass{
public void
show(){
System.out.println("x = "+x); }
}
}
class Test{
public static void
main(String[] args){
OuterClass o = new
OuterClass();
OuterClass.InnerClass inner = o.new InnerClass();
inner.show();
}
}
·
Anonymous class :
Java supports the definition
of un-named inner classes, these classes differ from the inner classes in only
one thing, which is that they are un-named, hence came the name “anonymous”.
Anonymous classes can have direct access to all of the class members of the
top-level-class in which they are defined.
Example : (using
abstract class)
abstract class
Person{
abstract void show();
}
class
NewClass{
public static void main(String[] args){
Person p = new Person() {
@Override
public void show() {
System.out.println("I am
in Show method");
}
};
p.show();
}
}
Example : (using
interface)
interface Person{
void show();
}
class
NewClass{
public static void main(String[] args){
Person p = new Person() {
@Override
public void show() {
System.out.println("I am
in Show method");
}
};
p.show();
}
}
Local Class :
Local inner class is a class which is created
inside a method body. If you want to access the local class you always need to
instantiate inside the method only. It can only access final member of the
class .
Example:
public class Test{
private int var_data = 30;
void display(){
class Local_Class{
public void msg(){
System.out.println(var_data);
}
}
Local_Class local = new Local_Class();
local.msg();
}
public static void main(String[] args){
Test t = new Test();
t.display();
}
}
Output:
30
Static Nested Class:
A static class that is created inside a class is called Static Nested class.A static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference. It can access static data members of outer class including private.
Example:
public class Test{
private static int var_data = 30;
static class Static_Inner_Class{
public void msg(){
System.out.println("private static data "+var_data);
}
}
public static void main(String[] args){
Test.Static_Inner_Class t = new Test.Static_Inner_Class();
t.msg();
}
}
Output:
private static data 30
Example (Using static method):
public class Test{
private static int var_data = 30;
static class Static_Inner_Class{
static void msg(){
System.out.println("private static data "+var_data);
}
}
public static void main(String[] args){
Test.Static_Inner_Class.msg();
}
}
Output:
private static data 30