Note 3.4.1.
Constructors must have the same name as the class! Constructors have no return type!
new followed by the name of the class being constructed, and then an argument list in parentheses. For example, here is how we create World, Turtle, and Person objects.
// To create a new object, write:
// ClassName variableName = new ConstructorName(arguments);
World world = new World();
Turtle t = new Turtle(world);
Person p = new Person("Pat", "[email protected]", "123-456-7890");
void, and instead of a method name, the name of the constructor is always the same as the name of the class. The constructors you write will be marked public. Like methods, constructors also have a parameter list specified in parenthesis that declare the variables that will be used to hold the arguments passed when the constructor is called.
public class ClassName
{
/* Instance variable declarations go here, before constructors */
/* Constructor - same name as Class, no return type */
public ClassName()
{
/* Implementation not shown */
}
/* Method definitions go here, after constructors */
}
new Person("Pat", "[email protected]", "123-456-7890") calls the Person constructor with the values to fill in for its name (“Pat”), email (”[email protected]”), and phone number (“123-456-7890”). When a constructor is called, memory is allocated for the object and the associated object reference is returned. Constructor parameters, if specified, provide data to initialize instance variables.
Person class from the last lesson.
public Person(String initName, String initEmail, String initPhone)
{
name = initName;
email = initEmail;
phoneNumber = initPhone;
}
name, email, and phoneNumber) in Person are initialized to the values provided by whatever code called the constructor. For example, in the constructor call new Person("Pat", "[email protected]", "123-456-7890"), the argument “Pat” is passed into the parameter variable initName, which the constructor then assigns to the instance variable name.
Person object without providing the three required String values.
Person that provides fill-in values like the following, if those make sense for your program. It’s up to you to decide if this is actually a useful value to have or if it would be better to force the users of the Person class to choose the values themselves using the constructor with arguments.
public Person()
{
name = "Anonymous";
email = "unknown";
phoneNumber = "unknown";
}
Fraction with the instance variables numerator and denominator. Complete the body of its 2 constructors. The no-argument constructor should set the default instance variable values to 1 rather than 0 since a fraction with 0 in the denominator is not valid. The constructor with parameters should copy those parameters into the instance variables. What will the code print out? Try tracing through it. View it in the Java visualizer by clicking on the Show CodeLens button below.
Car constructor. Then, fill in the code to call the constructor in the main method to create 2 Car objects. The first car should be a 2023 Ford and the second car should be a 2010 Honda. Run your program and make sure it works and prints out the information for both cars.
System.out.println to print out all the instance variables.
print() methods.
The default value of a reference type isnull.
public class Cat
{
private String name;
private int age;
private boolean isSenior;
public Cat(String n, int a)
{
name = n;
age = a;
if (age >= 10)
{
isSenior = true;
}
else
{
isSenior = false;
}
}
}
public class Cat
{
private String name;
private int age;
private int kittens;
public Cat(String n, int a, int k)
{
name = n;
age = a;
kittens = k;
}
public Cat(String n, int a)
{
name = n;
age = a;
kittens = 0;
}
/* Other methods not shown */
}
I. Cat c = new Cat("Sprinkles", 5, 0);
II. Cat c = new Cat("Lucy", 0, 5);
III. Cat c = new Cat("Luna", 5);
public class Cat
{
private String color;
private boolean isHungry;
/* missing constructor */
}
Cat c = new Cat("black", true);
public Cat(String c, boolean h)
{
c = "black";
h = true;
}
public Cat(String c, boolean h)
{
c = "black";
h = "true";
}
public Cat(String c, boolean h)
{
c = color;
h = isHungry;
}
public Cat(String c, boolean h)
{
color = black;
isHungry = true;
}
public Cat(String c, boolean h)
{
color = c;
isHungry = h;
}