Java Reference
In-Depth Information
new Rectangle(5, 10, 20, 30)
Here is what happens in detail.
1. The new operator makes a Rectangle object.
2. It uses the parameters (in this case, 5, 10, 20, and 30) to initialize the data of the
object.
3. It returns the object.
Usually the output of the new operator is stored in a variable. For example,
Rectangle box = new Rectangle(5, 10, 20, 30);
The process of creating a new object is called construction. The four values 5, 10, 20,
and 30 are called the construction parameters. Note that the new expression is not a
complete statement. You use the value of a new expression just like a method return
value: Assign it to a variable or pass it to another method.
Some classes let you construct objects in multiple ways. For example, you can also
obtain a Rectangle object by supplying no construction parameters at all (but you
must still supply the parentheses):
new Rectangle()
This expression constructs a (rather useless) rectangle with its top-left corner at the
origin (0, 0), width 0, and height 0.
S YNTAX 2.3 Object Construction
new ClassName(parameters)
Example:
new Rectangle(5, 10, 20, 30)
new Rectangle()
Purpose:
To construct a new object, initialize it with the construction parameters, and return
a reference to the constructed object
45
Search WWH ::




Custom Search