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.

Anonymous Classes

Anonymous classes enable you to make your code more concise.They enable you to declare and instantiate a class at the sametime. They are like local classes except that they do not have aname. Use them if you need to use a local class only once.

This section covers the following topics:

Declaring Anonymous Classes

While local classes are class declarations, anonymous classesare expressions, which means that you define the class in anotherexpression. The following example,HelloWorldAnonymousClasses, uses anonymous classes in the initialization statements of thelocal variablesfrenchGreeting andspanishGreeting, but uses a local class for theinitialization of the variableenglishGreeting:

public class HelloWorldAnonymousClasses {      interface HelloWorld {        public void greet();        public void greetSomeone(String someone);    }      public void sayHello() {                class EnglishGreeting implements HelloWorld {            String name = "world";            public void greet() {                greetSomeone("world");            }            public void greetSomeone(String someone) {                name = someone;                System.out.println("Hello " + name);            }        }              HelloWorld englishGreeting = new EnglishGreeting();                HelloWorld frenchGreeting = new HelloWorld() {            String name = "tout le monde";            public void greet() {                greetSomeone("tout le monde");            }            public void greetSomeone(String someone) {                name = someone;                System.out.println("Salut " + name);            }        };                HelloWorld spanishGreeting = new HelloWorld() {            String name = "mundo";            public void greet() {                greetSomeone("mundo");            }            public void greetSomeone(String someone) {                name = someone;                System.out.println("Hola, " + name);            }        };        englishGreeting.greet();        frenchGreeting.greetSomeone("Fred");        spanishGreeting.greet();    }    public static void main(String... args) {        HelloWorldAnonymousClasses myApp =            new HelloWorldAnonymousClasses();        myApp.sayHello();    }            }

Syntax of Anonymous Classes

As mentioned previously, an anonymous class is an expression.The syntax of an anonymous class expression is like theinvocation of a constructor, except that there is a class definitioncontained in a block of code.

Consider the instantiation of thefrenchGreeting object:

        HelloWorld frenchGreeting = new HelloWorld() {            String name = "tout le monde";            public void greet() {                greetSomeone("tout le monde");            }            public void greetSomeone(String someone) {                name = someone;                System.out.println("Salut " + name);            }        };

The anonymous class expression consists of the following:

Because an anonymous classdefinition is an expression, it must be part of a statement. Inthis example, the anonymous class expression is part of thestatement that instantiates thefrenchGreeting object. (Thisexplains why there is a semicolon after the closing brace.)

Accessing Local Variables of the Enclosing Scope, and Declaring and Accessing Members of the Anonymous Class

Like local classes, anonymous classes cancapture variables; they have the same access to local variables ofthe enclosing scope:

Anonymous classes also have the same restrictions as localclasses with respect to their members:

Note that you can declare the following in anonymous classes:

However, you cannot declare constructors in an anonymous class.

Examples of Anonymous Classes

Anonymous classes are often used in graphical userinterface (GUI) applications.

Consider the JavaFX exampleHelloWorld.java (from the sectionHello World, JavaFX Style fromGetting Started with JavaFX). Thissample creates a frame that contains aSay 'Hello World'button. The anonymous classexpression is highlighted:

import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.StackPane;import javafx.stage.Stage; public class HelloWorld extends Application {    public static void main(String[] args) {        launch(args);    }        @Override    public void start(Stage primaryStage) {        primaryStage.setTitle("Hello World!");        Button btn = new Button();        btn.setText("Say 'Hello World'");        btn.setOnAction(new EventHandler<ActionEvent>() {@Overridepublic void handle(ActionEvent event) {System.out.println("Hello World!");}});                StackPane root = new StackPane();        root.getChildren().add(btn);        primaryStage.setScene(new Scene(root, 300, 250));        primaryStage.show();    }}

In this example, the method invocationbtn.setOnAction specifies what happens when you select theSay 'Hello World' button. This method requires an object of typeEventHandler<ActionEvent>. TheEventHandler<ActionEvent> interface contains only one method, handle. Instead of implementing this method with a new class, the example uses an anonymous class expression. Notice that this expression is the argument passed to thebtn.setOnAction method.

Because theEventHandler<ActionEvent>interface contains only one method, you can use a lambdaexpression instead of an anonymous class expression. See thesectionLambda Expressions for moreinformation.

Anonymous classes are ideal for implementing an interface that contains two or more methods. The following JavaFX example is from the section Customization of UI Controls. The highlighted code creates a text field that only accepts numeric values. It redefines the default implementation of theTextField class with an anonymous class by overriding thereplaceText andreplaceSelection methods inherited from theTextInputControl class.

import javafx.application.Application;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.geometry.Insets;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.GridPane;import javafx.scene.layout.HBox;import javafx.stage.Stage;public class CustomTextFieldSample extends Application {        final static Label label = new Label();     @Override    public void start(Stage stage) {        Group root = new Group();        Scene scene = new Scene(root, 300, 150);        stage.setScene(scene);        stage.setTitle("Text Field Sample");         GridPane grid = new GridPane();        grid.setPadding(new Insets(10, 10, 10, 10));        grid.setVgap(5);        grid.setHgap(5);         scene.setRoot(grid);        final Label dollar = new Label("$");        GridPane.setConstraints(dollar, 0, 0);        grid.getChildren().add(dollar);                final TextField sum =new TextField() {@Overridepublic void replaceText(int start, int end, String text) {if (!text.matches("[a-z, A-Z]")) {super.replaceText(start, end, text);}label.setText("Enter a numeric value");}@Overridepublic void replaceSelection(String text) {if (!text.matches("[a-z, A-Z]")) {super.replaceSelection(text);}}};         sum.setPromptText("Enter the total");        sum.setPrefColumnCount(10);        GridPane.setConstraints(sum, 1, 0);        grid.getChildren().add(sum);                Button submit = new Button("Submit");        GridPane.setConstraints(submit, 2, 0);        grid.getChildren().add(submit);                submit.setOnAction(new EventHandler<ActionEvent>() {            @Override            public void handle(ActionEvent e) {                label.setText(null);            }        });                GridPane.setConstraints(label, 0, 1);        GridPane.setColumnSpan(label, 3);        grid.getChildren().add(label);                scene.setRoot(grid);        stage.show();    }     public static void main(String[] args) {        launch(args);    }}
« 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: Local Classes
Next page: Lambda Expressions

[8]ページ先頭

©2009-2025 Movatter.jp