+

Search Tips   |   Advanced Search


Constructors

Constructors declarations look like method declarations, except that they use the name of the class, and have no return type. For example...

To create new object myBike, call...

This creates space in memory for the object, and initializes the fields.

Here is a no-argument constructor...

To invoke...

Because they have different argument lists, both can be declared. Like methods, Java differentiates constructors on the basis of the number of arguments in the list, and their types. Do not write two constructors with the same number and type of arguments for the same class. The platform would not be able to tell them apart and causes a compile-time error.

The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor calls the no-argument constructor of the superclass. Verify the superclass has a no-argument constructor. If not, the compiler will complain. If the class has no explicit superclass, the implicit superclass of Object, which does have a no-argument constructor, is invoked.


Pass information to a method or a constructor

The declaration for a method or a constructor declares the number and the type of the arguments for that method or constructor.

Example method with four parameters...

At runtime the parameers will take on the values of the arguments that are passed in. Parameters refer to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked. Example method that array as an argument.

To pass a method into a method, use a lambda expression or a method reference.


Arbitrary Number of Arguments

To pass an arbitrary number of values to a method, use a varargs construct. The previous method could have used varargs rather than an array. To use varargs, add an ellipsis , then a space, and the parameter name. The method can then be called with any number of that parameter, including none.

varargs are commonly used with printing methods...

...which is called like this...

...or like this...

...or with yet a different number of arguments.

Like primitive data types, reference data type parameters, such as objects, are passed into methods by value. When the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method...

For example...

Let the method be invoked with these arguments:

Inside the method, circle initially refers to myCircle. The method changes the x and y coordinates of the object that circle references (i.e., myCircle) by 23 and 56, respectively. These changes will persist when the method returns. Then circle is assigned a reference to a new Circle object with x = y = 0. This reassignment has no permanence, however, because the reference was passed in by value and cannot change. Within the method, the object pointed to by circle has changed, but, when the method returns, myCircle still references the same Circle object as before the method was called.


Home