Constructor
We have been creating objects using the new operator and the class name with ().
Sphere3 sphere = new Sphere3();
The () make this look like a method, and it almost is: Sphere3() is an example of a constructor.
constructor¶
A special method we use with
newto create instances. It has the same name as the class of which it is a member.
Constructor Parameters¶
A constructor can take parameters and be overloaded.
We often use parameters to initialize fields.
The class below has two constructors, one of which initializes the radius field to the argument passed to the constructor.
public class Sphere4 {
public static double PI = 3.14159;
public double radius;
public Sphere4() { //NO-ARG CONSTRUCTOR
}
public Sphere4(double r) { //OVERLOADED CONSTRUCTOR
radius = r;
}
public double getVolume() {
double vol = 4.0 / 3.0 * PI * radius * radius * radius;
return vol;
}
public static double calculateCircumference(double r) {
return PI * 2 * r;
}
}
Practice Exercise¶
A constructor cannot have a return type. This class appears to define a one-argument constructor, but really just has a method with the same name as the class. This is legal, but don't do it.
public class Item2 { public double cost; public void Item2(double c) { cost = c; } }
Practice Exercise¶
When writing the word constructor, we may use the abbreviation ctor.
Drill¶
Objects/com.example.objs.drills.Dog* Add a constructor with parameters to initializebreedandweight. * Add a constructor with parameters to initializename,breed, andweight. * Create three dog instances and call thedisplayDogInfomethod on eachDogobject.Do not add a no-arg constructor.