Constructors
Constructors declarations look like method declarations, except that they use the name of the class, and have no return type. For example...
public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; }To create new object myBike, call...
Bicycle myBike = new Bicycle(30, 0, 8);
This creates space in memory for the object, and initializes the fields.
Here is a no-argument constructor...
public Bicycle() { gear = 1; cadence = 10; speed = 0; }To invoke...
Bicycle herBike = new Bicycle();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...
public double computePayment( double loanAmt, double rate, double futureValue, int numPeriods) { double interest = rate / 100.0; double partial1 = Math.pow((1 + interest), - numPeriods); double denominator = (1 - partial1) / interest; double answer = (-loanAmt / denominator) - ((futureValue * partial1) / denominator); return answer; }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.
public Polygon polygonFrom(Point[] corners) { // method body goes here }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.
public Polygon polygonFrom(Point... corners) { int numberOfSides = corners.length; double squareOfSide1, lengthOfSide1; squareOfSide1 = (corners[1].x - corners[0].x) * (corners[1].x - corners[0].x) + (corners[1].y - corners[0].y) * (corners[1].y - corners[0].y); lengthOfSide1 = Math.sqrt(squareOfSide1); // more method body code follows that creates and returns a // polygon connecting the Points }varargs are commonly used with printing methods...
public PrintStream printf(String format, Object... args)
...which is called like this...System.out.printf("%s: %d, %s%n", name, idnum, address);
...or like this...
System.out.printf("%s: %d, %s, %s, %s%n", name, idnum, address, phone, email);
...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...
public void moveCircle(Circle circle, int deltaX, int deltaY) { // Move origin of circle to x+deltaX, y+deltaY circle.setX(circle.getX() + deltaX); circle.setY(circle.getY() + deltaY); // Assign a new reference to circle circle = new Circle(0, 0); }Let the method be invoked with these arguments:
moveCircle(myCircle, 23, 56)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.