Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Inner Class

Java Inner Class

Java Objects and Classes Java Objects and Classes

In this chapter we will learn about Java's inner classes.

A class in Java can nest another class, with the following syntax format:

class OuterClass {   // 外部类
    // ...
    class NestedClass { // 嵌套类,或称为内部类
        // ...
    }
}

To access an inner class, you can create an object of the outer class and then create an object of the inner class.

There are two types of nested classes:

Non-static Inner Class

A non-static inner class is one class nested within another. It has access to the members of the outer class and is usually called an inner class.

Because the inner class is nested in the outer class, you must first instantiate the outer class, then create the inner class object.

Example

class

OuterClass

{

 

int

x

=

10

;

 

class

InnerClass

{

   

int

y

=

5

;

 

}

}

public

class

MyMainClass

{

 

public

static

void

main

(

String

[

]

args

)

{

    OuterClass myOuter

=

new

OuterClass

(

)

;

    OuterClass.

InnerClass

myInner

=

myOuter.

new

InnerClass

(

)

;

   

System

.

out

.

println

(

myInner.

y

myOuter.

x

)

;

 

}

}

The output of the above example is:

15

Private Inner Class

An inner class can be modified with private or protected. If you do not want the inner class to be accessed by the outer class, you can use the private modifier:

Example

class

OuterClass

{

 

int

x

=

10

;

 

private

class

InnerClass

{

   

int

y

=

5

;

 

}

}

public

class

MyMainClass

{

 

public

static

void

main

(

String

[

]

args

)

{

    OuterClass myOuter

=

new

OuterClass

(

)

;

    OuterClass.

InnerClass

myInner

=

myOuter.

new

InnerClass

(

)

;

   

System

.

out

.

println

(

myInner.

y

myOuter.

x

)

;

 

}

}

In the example above, InnerClass is set as a private inner class, and execution will report an error:

MyMainClass.java:12: error: OuterClass.InnerClass has private access in OuterClass
    OuterClass.InnerClass myInner = myOuter.new InnerClass();
             ^

Static Inner Class

A static inner class can be defined using the static keyword; for a static inner class, we do not need to create the outer class to access it and can access it directly:

Example

class

OuterClass

{

 

int

x

=

10

;

 

static

class

InnerClass

{

   

int

y

=

5

;

 

}

}

public

class

MyMainClass

{

 

public

static

void

main

(

String

[

]

args

)

{

    OuterClass.

InnerClass

myInner

=

new

OuterClass.

InnerClass

(

)

;

   

System

.

out

.

println

(

myInner.

y

)

;

 

}

}

The output of the above example is:

5

**Note:** A static inner class cannot access the members of the outer class.

Accessing Outer Class Members from an Inner Class

An advanced use of inner classes is that they can access the attributes and methods of the outer class:

Example

class

OuterClass

{

 

int

x

=

10

;

 

class

InnerClass

{

   

public

int

myInnerMethod

(

)

{

     

return

x

;

   

}

 

}

}

public

class

MyMainClass

{

 

public

static

void

main

(

String

[

]

args

)

{

    OuterClass myOuter

=

new

OuterClass

(

)

;

    OuterClass.

InnerClass

myInner

=

myOuter.

new

InnerClass

(

)

;

   

System

.

out

.

println

(

myInner.

myInnerMethod

(

)

)

;

 

}

}

The output of the above example is:

10

Java Objects and Classes Java Objects and Classes