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: More on 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.

Returning a Value from a Method

A method returns to the code that invoked it when it

whichever occurs first.

You declare a method's return type in its method declaration. Within the body of the method, you use thereturn statement to return the value.

Any method declaredvoid doesn't return a value. It does not need to contain areturn statement, but it may do so. In such a case, areturn statement can be used to branch out of a control flow block and exit the method and is simply used like this:

return;

If you try to return a value from a method that is declaredvoid, you will get a compiler error.

Any method that is not declaredvoid must contain areturn statement with a corresponding return value, like this:

return returnValue;

The data type of the return value must match the method's declared return type; you can't return an integer value from a method declared to return a boolean.

ThegetArea() method in theRectangleRectangle class that was discussed in the sections on objects returns an integer:

    // a method for computing the area of the rectangle    public int getArea() {        return width * height;    }

This method returns the integer that the expressionwidth*height evaluates to.

ThegetArea method returns a primitive type. A method can also return a reference type. For example, in a program to manipulateBicycle objects, we might have a method like this:

public Bicycle seeWhosFastest(Bicycle myBike, Bicycle yourBike,                              Environment env) {    Bicycle fastest;    // code to calculate which bike is     // faster, given each bike's gear     // and cadence and given the     // environment (terrain and wind)    return fastest;}

Returning a Class or Interface

If this section confuses you, skip it and return to it after you have finished the lesson on interfaces and inheritance.

When a method uses a class name as its return type, such aswhosFastest does, the class of the type of the returned object must be either a subclass of, or the exact class of, the return type. Suppose that you have a class hierarchy in whichImaginaryNumber is a subclass ofjava.lang.Number, which is in turn a subclass ofObject, as illustrated inthe following figure.

The class hierarchy for ImaginaryNumber

The class hierarchy for ImaginaryNumber

Now suppose that you have a method declared to return aNumber:

public Number returnANumber() {    ...}

ThereturnANumber method can return anImaginaryNumber but not anObject.ImaginaryNumber is aNumber because it's a subclass ofNumber. However, anObject is not necessarily aNumber — it could be aString or another type.

You can override a method and define it to return a subclass of the original method, like this:

public ImaginaryNumber returnANumber() {    ...}

This technique, calledcovariant return type, means that the return type is allowed to vary in the same direction as the subclass.


Note: You also can use interface names as return types. In this case, the object returned must implement the specified interface.
« 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: More on Classes
Next page: Using the this Keyword

[8]ページ先頭

©2009-2025 Movatter.jp