GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing docs.oracle.com — Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects) Documentation
The Java™ Tutorials
Hide TOC
Returning a Value from a Method
Trail: Learning the Java Language
Lesson: Classes and Objects Section: More on Classes 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. Returning a Value from a MethodA 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 the Any method declared return; If you try to return a value from a method that is declared Any method that is not declared 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. The // a method for computing the area of the rectangle
public int getArea() {
return width * height;
}
This method returns the integer that the expression The 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 InterfaceIf 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 as ![]() The class hierarchy for ImaginaryNumber Now suppose that you have a method declared to return a public Number returnANumber() {
...
}
The You can override a method and define it to return a subclass of the original method, like this: public ImaginaryNumber returnANumber() {
...
}
This technique, called covariant 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. 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 |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |