Nested Class
What is Nested Class?
 Inner classes are class within Class.
 Inner class instance has special relationship with
Outer class. This special relationship gives inner class
access to member of outer class as if they are the
part of outer class.
 Additionally, it can access all the members of outer
class including private data members and methods.
Syntax of InnerClass
//outer class
class OuterClass {
//inner class
class InnerClass {
}
}
How to access Inner Class?
 Inner class can be accessed only through live
instance of outer class.
Example of InnerClass
Types of Nested class:
non-static nested class(inner class)
a)Member inner class
b)Anonymous inner class
c)Local inner class
static nested class
Member Inner Class
 A class that is declared inside a class but outside a
method is known as member inner class.
Invocation of Member Inner class
 From within the class
 From outside the class
Example of member inner class that is
invoked inside a class
Example of member inner class that is
invoked outside a class
Anonymous inner class
 A class that have no name is known as anonymous
inner class.
Anonymous class can be created by:
Class (may be abstract class also).
Interface
Example of anonymous inner class by
abstract class
Example of anonymous inner class by
interface
Local inner class
 A class that is created inside a method is known as local
inner class.
 If you want to invoke the methods of local inner class,
you must instantiate this class inside the method.
Rules for Local Inner class
 Local variable can't be private, public or protected.
 Local inner class cannot be invoked from outside the
method.
 Local inner class cannot access non-final local variable.
Example of local inner class
static nested class
 A static class that is created inside a class is known as
static nested class.
 It can access static data members of outer class
including private.
 static nested class cannot access non-static (instance)
data member or method.
Example of static nested class
Purpose of nested classes
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.
Nested classes are used to develop more
readable and maintainable code because it
logically group classes and interfaces in one place
only.
Code Optimization: It requires less code to write.
Reference:
 http://www.studytonight.com/java/nested-
classes.php
 http://www.javatpoint.com/difference-between-
nested-classes-and-inner-classes
 http://www.javatpoint.com/annonymous-inner-class
 http://www.javatpoint.com/member-inner-class
 http://examples.javacodegeeks.com/core-
java/class/static-inner-class-example/
Thank You!!!

Inner class

  • 1.
  • 2.
    What is NestedClass?  Inner classes are class within Class.  Inner class instance has special relationship with Outer class. This special relationship gives inner class access to member of outer class as if they are the part of outer class.  Additionally, it can access all the members of outer class including private data members and methods.
  • 3.
    Syntax of InnerClass //outerclass class OuterClass { //inner class class InnerClass { } } How to access Inner Class?  Inner class can be accessed only through live instance of outer class.
  • 4.
  • 5.
    Types of Nestedclass: non-static nested class(inner class) a)Member inner class b)Anonymous inner class c)Local inner class static nested class
  • 6.
    Member Inner Class A class that is declared inside a class but outside a method is known as member inner class. Invocation of Member Inner class  From within the class  From outside the class
  • 7.
    Example of memberinner class that is invoked inside a class
  • 8.
    Example of memberinner class that is invoked outside a class
  • 9.
    Anonymous inner class A class that have no name is known as anonymous inner class. Anonymous class can be created by: Class (may be abstract class also). Interface
  • 10.
    Example of anonymousinner class by abstract class
  • 11.
    Example of anonymousinner class by interface
  • 12.
    Local inner class A class that is created inside a method is known as local inner class.  If you want to invoke the methods of local inner class, you must instantiate this class inside the method. Rules for Local Inner class  Local variable can't be private, public or protected.  Local inner class cannot be invoked from outside the method.  Local inner class cannot access non-final local variable.
  • 13.
    Example of localinner class
  • 14.
    static nested class A static class that is created inside a class is known as static nested class.  It can access static data members of outer class including private.  static nested class cannot access non-static (instance) data member or method.
  • 15.
    Example of staticnested class
  • 16.
    Purpose of nestedclasses 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. Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only. Code Optimization: It requires less code to write.
  • 17.
    Reference:  http://www.studytonight.com/java/nested- classes.php  http://www.javatpoint.com/difference-between- nested-classes-and-inner-classes http://www.javatpoint.com/annonymous-inner-class  http://www.javatpoint.com/member-inner-class  http://examples.javacodegeeks.com/core- java/class/static-inner-class-example/
  • 18.