BANSARI SHAH
150410107098
CE-2, BATCH B
JAVA
NESTED CLASS
 In Java, just like methods, variables of a class too can have
another class as its member. Writing a class within another
is allowed in Java. The class written within is called
the nested class, and the class that holds the inner class is
called the outer class.
Syntax to write nested class:
Class Outer_class
{
Class Inner_class
{
}
}
Nested classes are
divided into two types
−
 Non-static nested
classes − These are
the non-static
members of a class.
 Static nested
classes − These are
the static members of
a class.
NON-STATIC NESTED
CLASSES
 Inner classes are a security mechanism in Java.
We know a class cannot be associated with the
access modifier private, but if we have the class as
a member of other class, then the inner class can
be made private. And this is also used to access
the private members of a class.
 Inner classes are of three types depending on how
and where you define them. They are −
1. Inner Class
2. Method-local Inner Class
3. Anonymous Inner Class
1. INNER CLASS
 Creating an inner class is quite simple. You just
need to write a class within a class. Unlike a class,
an inner class can be private and once you declare
an inner class private, it cannot be accessed from
an object outside the class.
 Following is the program to create an inner class
and access it. In the given example, we make the
inner class private and access the class through a
method.
ACCESSING THE PRIVATE
MEMBERS
 As mentioned earlier, inner classes are also used to
access the private members of a class. Suppose, a class
is having private members to access them. Write an
inner class in it, return the private members from a
method within the inner class, say, getValue(), and finally
from another class (from which you want to access the
private members) call the getValue() method of the inner
class.
 To instantiate the inner class, initially you have to
instantiate the outer class. Thereafter, using the object
of the outer class, following is the way in which you can
instantiate the inner class.
Outer_Demo outer = new Outer_Demo();
Outer_Demo.Inner_Demo inner = outer.new Inner_Demo();
2. METHOD-LOCAL INNER
CLASS
 In Java, we can write a class within a method and
this will be a local type. Like local variables, the
scope of the inner class is restricted within the
method.
 A method-local inner class can be instantiated only
within the method where the inner class is defined.
The following program shows how to use a
method-local inner class.
3. ANONYMOUS INNER
CLASS
 An inner class declared without a class name is known as
an anonymous inner class. In case of anonymous inner
classes, we declare and instantiate them at the same time.
Generally, they are used whenever you need to override the
method of a class or an interface. The syntax of an
anonymous inner class is as follows −
 Syntax
AnonymousInner an_inner = new AnonymousInner()
{
public void my_method()
{ ........
........
}
};
Inner class
Inner class

Inner class

  • 1.
  • 2.
    NESTED CLASS  InJava, just like methods, variables of a class too can have another class as its member. Writing a class within another is allowed in Java. The class written within is called the nested class, and the class that holds the inner class is called the outer class. Syntax to write nested class: Class Outer_class { Class Inner_class { } }
  • 3.
    Nested classes are dividedinto two types −  Non-static nested classes − These are the non-static members of a class.  Static nested classes − These are the static members of a class.
  • 4.
    NON-STATIC NESTED CLASSES  Innerclasses are a security mechanism in Java. We know a class cannot be associated with the access modifier private, but if we have the class as a member of other class, then the inner class can be made private. And this is also used to access the private members of a class.  Inner classes are of three types depending on how and where you define them. They are − 1. Inner Class 2. Method-local Inner Class 3. Anonymous Inner Class
  • 5.
    1. INNER CLASS Creating an inner class is quite simple. You just need to write a class within a class. Unlike a class, an inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class.  Following is the program to create an inner class and access it. In the given example, we make the inner class private and access the class through a method.
  • 7.
    ACCESSING THE PRIVATE MEMBERS As mentioned earlier, inner classes are also used to access the private members of a class. Suppose, a class is having private members to access them. Write an inner class in it, return the private members from a method within the inner class, say, getValue(), and finally from another class (from which you want to access the private members) call the getValue() method of the inner class.  To instantiate the inner class, initially you have to instantiate the outer class. Thereafter, using the object of the outer class, following is the way in which you can instantiate the inner class. Outer_Demo outer = new Outer_Demo(); Outer_Demo.Inner_Demo inner = outer.new Inner_Demo();
  • 9.
    2. METHOD-LOCAL INNER CLASS In Java, we can write a class within a method and this will be a local type. Like local variables, the scope of the inner class is restricted within the method.  A method-local inner class can be instantiated only within the method where the inner class is defined. The following program shows how to use a method-local inner class.
  • 11.
    3. ANONYMOUS INNER CLASS An inner class declared without a class name is known as an anonymous inner class. In case of anonymous inner classes, we declare and instantiate them at the same time. Generally, they are used whenever you need to override the method of a class or an interface. The syntax of an anonymous inner class is as follows −  Syntax AnonymousInner an_inner = new AnonymousInner() { public void my_method() { ........ ........ } };