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
Section: Nested Classes
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.

Inner Class Example

To see an inner class in use, first consider an array. In the following example, you create an array, fill it with integer values, and then output only values of even indices of the array in ascending order.

TheDataStructure.java example that follows consists of:

 public class DataStructure {        // Create an array    private final static int SIZE = 15;    private int[] arrayOfInts = new int[SIZE];        public DataStructure() {        // fill the array with ascending integer values        for (int i = 0; i < SIZE; i++) {            arrayOfInts[i] = i;        }    }        public void printEven() {                // Print out values of even indices of the array        DataStructureIterator iterator = this.new EvenIterator();        while (iterator.hasNext()) {            System.out.print(iterator.next() + " ");        }        System.out.println();    }        interface DataStructureIterator extends java.util.Iterator<Integer> { }     // Inner class implements the DataStructureIterator interface,    // which extends the Iterator<Integer> interface        private class EvenIterator implements DataStructureIterator {                // Start stepping through the array from the beginning        private int nextIndex = 0;                public boolean hasNext() {                        // Check if the current element is the last in the array            return (nextIndex <= SIZE - 1);        }                        public Integer next() {                        // Record a value of an even index of the array            Integer retValue = Integer.valueOf(arrayOfInts[nextIndex]);                        // Get the next even element            nextIndex += 2;            return retValue;        }    }        public static void main(String s[]) {                // Fill the array with integer values and print out only        // values of even indices        DataStructure ds = new DataStructure();        ds.printEven();    }}

The output is:

0 2 4 6 8 10 12 14

Note that theEvenIterator class refers directly to thearrayOfInts instance variable of theDataStructure object.

You can use inner classes to implement helper classes such as the one shown in the this example. To handle user interface events, you must know how to use inner classes, because the event-handling mechanism makes extensive use of them.

Local and Anonymous Classes

There are two additional types of inner classes. You can declare an inner class within the body of a method. These classes are known aslocal classes. You can also declare an inner class within the body of a method without naming the class. These classes are known asanonymous classes.

Modifiers

You can use the same modifiers for inner classes that you use for other members of the outer class. For example, you can use the access specifiersprivate,public, andprotected to restrict access to inner classes, just as you use them to restrict access do to other class members.

« 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: Nested Classes
Next page: Local Classes

[8]ページ先頭

©2009-2025 Movatter.jp