Title: Overview
1Inner Classes
- Overview
- Java Inner Classes A Definition.
- Overview of Nested Classes.
- Top level Nested Classes.
- Non-static Inner Classes.
- Non-static Inner Class Example.
- Inner Classes An Examples.
- Coming Next Anonymous Classes.
2Overview of Nested Classes
An ordinary class or interface is defined at the
top level, which is the package level. These
classes and interfaces(also called top-level
package member classes or interfaces) are grouped
into packages. In addition to these top-level
package member classes and interfaces, there are
four categories of nested classes and one of
nested interfaces defined by the context these
classes and interfaces are declared
in 1-Top-level Nested Classes and interfaces 2-
Non-static InnerClasses 3- Local Classes 4-
Anonymous Classes
Inner classes
3Overview of Nested Classes (Contd)
Top-level nested classes and interfaces are also
considered to be at the top level. Inner classes
can use simple non-static names from the
enclosing context, i.e. an instance of an inner
class is not limited to directly accessing only
its own instance variables. An instance of an
inner class may be associated with an instance of
the enclosing class and may access its members A
top-level nested class is defined as a static
member in a top-level(possibly nested)class .
Such a nested class can be instantiated like any
ordinary top-level class, using its full name. No
instance of the enclosing class is required to
instantiate a top-level nested classes.
4Top-Level Nested Classes
//TopLevel class public class TopLevelClass
///................ static class
NestedTopLevelClass ///...............
interface NestedTopLevelInterface1
//.............. static class
NestedTopLevelClass1 implements
NestedTopLevelInterface1 //..........
Note that each nested top-level class is defined
as static, just like static variables and methods
in a class. The full name of the nested
top-level class is
TopLevelClass.NestedTo
pLevelClass.NestedTopLevelClass1 The full name of
the nested top-level interface NestedTopLevelInter
face1 is TopLevelClass.Neste
dTopLevelClass.NestedTopLevelInterface1
5Top-Level Nested Classes (Contd)
If the file TopLevelClass.java is compiled, it
will result in the generation of the following
class files, where each file corresponds to a
class or interface definition public class
TopLevelClass ///................ static
class NestedTopLevelClass ///...............
interface NestedTopLevelInterface1
//.............. static class
NestedTopLevelClass1 implements
NestedTopLevelInterface1 //..........
TopLevelClassNestedTopLevelClassNestedTo
pLevelClass1.class TopLevelClassNestedTopLevelCla
ssNestedTopLevelInterface1.class TopLevelClassNe
stedTopLevelClass.class TopLevelClass.class Note
how the full class name, corresponds to the
filename(minus the extension) with the dollar
sign()replaced the the dot sign(.)
6Two Non-Static Inner Classes
- Non-static inner classes are defined without the
keyword static, as members of an enclosing class,
and can also be nested to any depth. Non-static
inner classes are on par with other non-static
members defined in a classes. The following
aspects about non-static inner classes should be
noted - An instance of a non-static inner class can only
exist with an instance of its enclosing class.
This means that an instance of a non-static inner
class must be created in the context of an
instance of the enclosing class. This also means
that a non-static inner class cannot have static
members. In other words, the class does not
provide any services , only instances of the
class do. - Methods of a non-static inner class can directly
refer to any member(including classes)of any
enclosing class, including private members. No
explicit reference is required. - Since a non-static inner class is a member of an
enclosing class, it can have any accessibility
public, default, protected or private
7Inner Classes
// ShowInnerClass.javaDemonstrate using inner
classes public class ShowInnerClass
private int data public void m() // Outer
class method // Do
something InnerClass instance new
InnerClasss() class
InnerClass // An inner class public
void mi() // A method in the inner class
// Directly reference data and method
defined in its outer class data m()
// End of inner class
- An inner class, or nested class, is a class
defined within the scope of another class. - The class InnerClass is defined inside
ShowInnerClass. This inner class is just like any
regular class - An inner class can reference the data and
methods defined in the outer class in which it
nests, so you do not need to pass the reference
of the outer class to the constructor of the
inner class - An inner class is only for supporting the work
of its containing outer class and is compiled
into a class named OutClassNameInnerClassName.cla
ss. For example, the inner class classInnerClass
in ShowInnerClass is compiled into
ShowInnerClassInnerClass.class
8Declaring Inner Classes in Java Programs
- public class Parcel class Contents
private int i 11 public int value()
return i // End of Inner class
Contents - class Destination private String
label Destination(String whereTo)
label whereTo // End of Destination
constructor String readLabel()
return label // End of method
readLabel() // End of Inner class Destination
- // End of class Parcel
9Inner Classes An Example
We can add more functionality to the class Parcel
to get a complete running Java program as follows
- class Parcel class Contents private int
postOfficeBox 11 public int value()
return postOfficeBox // End of
inner class Contents - class Destination private String
label Destination(String whereTo)
label whereTo // End of Destination
constructorString readLabel() return
label // End of method readLabel() - // End of inner class Destination
public void ship(String dest) Contents c
new Contents()Destination d new
Destination(dest) System.out.println(P.
O. Box c.value() Algiers)
System.out.println(d.readLabel()) // End
of method ship() // End of Outer class
Parcel public class TestParcel public
static void main(String args) Parcel p new
Parcel1()p.ship(Algeria") // End of
method main() // End of TestParcel class
10Inner Classes An Example (Contd)
The class Parcel has a method called ship() that
makes use of the inner classes Contents and
Destination as any usual class.
Contents c new Contents() Destination d new
Destination()
So, inner classes behave like any ordinary class
in Java? No there is a difference as shown below.
Lets add other methods to the outer class Parcel
and modify the methods ship() such as
// to() New method added to Parcel class public
Destination to(String s) return new
Destination(s) // cont() New method added to
Parcel class public Contents cont() return
new Contents() // End of method cont()
// Modified ship() method public void ship(String
dest) Contents c cont() Destination d
to(dest) System.out.println(d.readLabel())
// End of method ship()
11Inner Classes An Example (Contd)
Similarly, we can modify the main() method of the
test class as follows
// Modified main() method public static void
main(String args) Parcel p new
Parcel() p.ship(Algeria") Parcel q new
Parcel() // Defining references to inner
classes Parcel.Contents c q.cont() Parcel.Dest
ination d q.to(Saudi Arabia") // End of
method main()
Fully Qualified Name
To create an object of the inner class anywhere
except from within a non-static method of the
outer class, the type of the object must be
specified as OuterClassName.InnerClassName, as
seen in the main() method above.
12Non-Static Inner Class Example 2
class ToplevelClass
// (1) private static String
msg "Shine the inner light." // (2)
public NonStaticInnerClass makeInstance()
// (3) return new
NonStaticInnerClass() //
(4) public class NonStaticInnerClass
// (5) // private
static int staticVar
// (6) Not OK. private String string
// (7)
public NonStaticInnerClass() string msg
// (8) public void printMsg()
System.out.println(string) // (9)
public class Client
// (10) public static void
main(String args) // (11)
ToplevelClass topRef new
ToplevelClass() // (12)
ToplevelClass.NonStaticInnerClass innerRef1
topRef.makeInstance()
// (13) innerRef1.printMsg()
// (14) //
ToplevelClass.NonStaticInnerClass innerRef2
// new ToplevelClass.NonStaticInne
rClass() // (15) Not OK.
ToplevelClass.NonStaticInnerClass innerRef3
topRef.new NonStaticInnerClass(
) // (16)
Top Level Class
13Non-Static Inner Class Example 2 (Contd)
- Defines a non-static inner class. (5) Declaration
of a static variable in class NonStaticInnerClass
would be flagged as a compile time error, as a
non-static inner class cannot define static
members. The non-static method makeInstance() at
(3) in the class ToplevelClass creates an
instance of NonStaticInnerClass class using the
new operator, - (4) this creates an instance of a non-static
inner class in the context, of the instance of
the enclosing class on which the makeInstance()
method is invoked. - (15) An attempt to create an instance of the
non-static inner class, without an outer
instance, using the new operator with the full
name of the inner class. Result compile time
error - (16) A special form of the new operator must be
used topRef.new