Movatterモバイル変換


[0]ホーム

URL:


Documentation

The Java™ Tutorials
Classes and Objects
Classes
Declaring Classes
Declaring Member Variables
Defining Methods
Providing Constructors for Your Classes
Passing Information to a Method or a Constructor
Objects
Creating Objects
Using Objects
More on Classes
Returning a Value from a Method
Using the this Keyword
Controlling Access to Members of a Class
Understanding Class Members
Initializing Fields
Summary of Creating and Using Classes and Objects
Questions and Exercises
Questions and Exercises
Nested Classes
Inner Class Example
Local Classes
Anonymous Classes
Lambda Expressions
Method References
When to Use Nested Classes, Local Classes, Anonymous Classes, and Lambda Expressions
Questions and Exercises
Enum Types
Questions and Exercises
Trail: Learning the Java Language
Lesson: Classes and Objects
Home Page >Learning the Java Language >Classes and Objects
« Previous • Trail • Next »

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
SeeDev.java for updated tutorials taking advantage of the latest releases.
SeeJava Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
SeeJDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.

Nested Classes

The Java programming language allows you to define a class within another class. Such a class is called anested class and is illustrated here:

class OuterClass {    ...    class NestedClass {        ...    }}

Terminology: Nested classes are divided into two categories: non-static and static. Non-static nested classes are calledinner classes. Nested classes that are declaredstatic are calledstatic nested classes.
class OuterClass {    ...    class InnerClass {        ...    }    static class StaticNestedClass {        ...    }}

A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. As a member of theOuterClass, a nested class can be declaredprivate,public,protected, orpackage private. (Recall that outer classes can only be declaredpublic orpackage private.)

Why Use Nested Classes?

Compelling reasons for using nested classes include the following:

Inner Classes

As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

Objects that are instances of an inner class existwithin an instance of the outer class. Consider the following classes:

class OuterClass {    ...    class InnerClass {        ...    }}

An instance ofInnerClass can exist only within an instance ofOuterClass and has direct access to the methods and fields of its enclosing instance.

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

OuterClass outerObject = new OuterClass();OuterClass.InnerClass innerObject = outerObject.new InnerClass();

There are two special kinds of inner classes:local classes andanonymous classes.

Static Nested Classes

As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, 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.Inner Class and Nested Static Class Example demonstrates this.


Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.Inner Class and Nested Static Class Example also demonstrates this.

You instantiate a static nested class the same way as a top-level class:

StaticNestedClass staticNestedObject = new StaticNestedClass();

Inner Class and Nested Static Class Example

The following example,OuterClass, along withTopLevelClass, demonstrates which class members ofOuterClass an inner class (InnerClass), a nested static class (StaticNestedClass), and a top-level class (TopLevelClass) can access:

OuterClass.java

public class OuterClass {    String outerField = "Outer field";    static String staticOuterField = "Static outer field";    class InnerClass {        void accessMembers() {            System.out.println(outerField);            System.out.println(staticOuterField);        }    }    static class StaticNestedClass {        void accessMembers(OuterClass outer) {            // Compiler error: Cannot make a static reference to the non-static            //     field outerField            // System.out.println(outerField);            System.out.println(outer.outerField);            System.out.println(staticOuterField);        }    }    public static void main(String[] args) {        System.out.println("Inner class:");        System.out.println("------------");        OuterClass outerObject = new OuterClass();        OuterClass.InnerClass innerObject = outerObject.new InnerClass();        innerObject.accessMembers();        System.out.println("\nStatic nested class:");        System.out.println("--------------------");        StaticNestedClass staticNestedObject = new StaticNestedClass();                staticNestedObject.accessMembers(outerObject);                System.out.println("\nTop-level class:");        System.out.println("--------------------");        TopLevelClass topLevelObject = new TopLevelClass();                topLevelObject.accessMembers(outerObject);                    }}

TopLevelClass.java

public class TopLevelClass {    void accessMembers(OuterClass outer) {             // Compiler error: Cannot make a static reference to the non-static        //     field OuterClass.outerField        // System.out.println(OuterClass.outerField);        System.out.println(outer.outerField);        System.out.println(OuterClass.staticOuterField);    }  }

This example prints the following output:

Inner class:------------Outer fieldStatic outer fieldStatic nested class:--------------------Outer fieldStatic outer fieldTop-level class:--------------------Outer fieldStatic outer field

Note that a static nested class interacts with the instance members of its outer class just like any other top-level class. The static nested classStaticNestedClass can't directly accessouterField because it's an instance variable of the enclosing class,OuterClass. The Java compiler generates an error at the highlighted statement:

static class StaticNestedClass {    void accessMembers(OuterClass outer) {       // Compiler error: Cannot make a static reference to the non-static       //     field outerFieldSystem.out.println(outerField);    }}

To fix this error, accessouterField through an object reference:

System.out.println(outer.outerField);

Similarly, the top-level classTopLevelClass can't directly accessouterField either.

Shadowing

If a declaration of a type (such as a member variable or a parameter name) in a particular scope (such as an inner class or a method definition) has the same name as another declaration in the enclosing scope, then the declarationshadows the declaration of the enclosing scope. You cannot refer to a shadowed declaration by its name alone. The following example,ShadowTest, demonstrates this:

 public class ShadowTest {    public int x = 0;    class FirstLevel {        public int x = 1;        void methodInFirstLevel(int x) {            System.out.println("x = " + x);            System.out.println("this.x = " + this.x);            System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);        }    }    public static void main(String... args) {        ShadowTest st = new ShadowTest();        ShadowTest.FirstLevel fl = st.new FirstLevel();        fl.methodInFirstLevel(23);    }}

The following is the output of this example:

x = 23this.x = 1ShadowTest.this.x = 0

This example defines three variables namedx: the member variable of the classShadowTest, the member variable of the inner classFirstLevel, and the parameter in the methodmethodInFirstLevel. The variablex defined as a parameter of the methodmethodInFirstLevel shadows the variable of the inner classFirstLevel. Consequently, when you use the variablex in the methodmethodInFirstLevel, it refers to the method parameter. To refer to the member variable of the inner classFirstLevel, use the keywordthis to represent the enclosing scope:

System.out.println("this.x = " + this.x);

Refer to member variables that enclose larger scopes by the class name to which they belong. For example, the following statement accesses the member variable of the classShadowTest from the methodmethodInFirstLevel:

System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);

Serialization

Serialization of inner classes, includinglocal andanonymous classes, is strongly discouraged. When the Java compiler compiles certain constructs, such as inner classes, it createssynthetic constructs; these are classes, methods, fields, and other constructs that do not have a corresponding construct in the source code. Synthetic constructs enable Java compilers to implement new Java language features without changes to the JVM. However, synthetic constructs can vary among different Java compiler implementations, which means that.class files can vary among different implementations as well. Consequently, you may have compatibility issues if you serialize an inner class and then deserialize it with a different JRE implementation. See the sectionImplicit and Synthetic Parameters in the sectionObtaining Names of Method Parameters for more information about the synthetic constructs generated when an inner class is compiled.

« PreviousTrailNext »

About Oracle |Contact Us |Legal Notices |Terms of Use |Your Privacy Rights

Copyright © 1995, 2024 Oracle and/or its affiliates. All rights reserved.

Previous page: Questions and Exercises: Objects
Next page: Inner Class Example

[8]ページ先頭

©2009-2025 Movatter.jp