A method returns to the code that invoked it when it
completes all the statements in the method,
reaches a return statement, or
throws an exception (covered later),
whichever occurs first.
You declare a method's return type in its method declaration. Within the body of the method, you use the return statement to return the value.
Any method declared void doesn't return a value.
It does not need to contain a return statement, but it may do so.
In such a case, a return 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 declared void, you will get a compiler error.
Any method that is not declared void must contain a return 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.
The getArea() method below returns an integer:
int getArea(int x, int y)
{
width=x;
height=y;
return width*height;
}
This method returns the integer that the expression width*height evaluates to.
The getArea method returns a primitive type.
The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.
For example, the Point class was written like this
public class Point {
public int x = 0;
public int y = 0;
//constructor
public Point(int a, int b) {
x = a;
y = b;
}
}
but it could have been written like this:
public class Point {
public int x = 0;
public int y = 0;
//constructor
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
Each argument to the constructor shadows one of the object's fields — inside the constructor x is a local copy of the constructor's first argument.
To refer to the Point field x, the constructor must use this.x.
Using this with a Constructor
From within a constructor, you can also use the this keyword to call another constructor in the same class.
Doing so is called an explicit constructor invocation. Here's another Rectangle class, with a different implementation from the one in the Objects section.
public class Rectangle {
private int x, y;
private int width, height;
public Rectangle() {
this(0, 0, 1, 1);
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}
This class contains a set of constructors.
Each constructor initializes some or all of the rectangle's member variables.
The constructors provide a default value for any member variable whose initial value is not provided by an argument.
For example, the no-argument constructor creates a 1x1 Rectangle at coordinates 0,0.
The two-argument constructor calls the four-argument constructor, passing in the width and height but always using the 0,0 coordinates.
As before, the compiler determines which constructor to call, based on the number and the type of arguments.
If present, the invocation of another constructor must be the first line in the constructor.