Lists Review and Performance : 03/31/2023
Linked Lists : 03/30/2023
Algorithms and Lists : 03/29/2023
Lambda Expressions : 03/24/2023
Anonymous Classes : 03/23/2023
Practice with Interfaces : 03/22/2023
Implementing Interfaces : 03/21/2023
Using Interfaces : 03/20/2023
Working with Exceptions : 03/10/2023
Throwing Exceptions : 03/09/2023
Catching Exceptions : 03/08/2023
References and Polymorphism : 03/07/2023
References : 03/06/2023
Data Modeling 2 : 03/03/2023
Equality and Object Copying : 03/02/2023
Polymorphism : 03/01/2023
Inheritance : 02/28/2023
Data Modeling 1 : 02/27/2023
Static : 02/24/2023
Encapsulation : 02/23/2023
Constructors : 02/22/2023
Objects, Continued : 02/21/2023
Introduction to Objects : 02/20/2023
Compilation and Type Inference : 02/17/2023
Practice with Collections : 02/16/2023
Maps and Sets : 02/15/2023
Lists and Type Parameters : 02/14/2023
Imports and Libraries : 02/13/2023
Multidimensional Arrays : 02/10/2023
Practice with Strings : 02/09/2023
null : 02/08/2023
Algorithms and Strings : 02/07/2023
Strings : 02/06/2023
Functions and Algorithms : 02/03/2023
Practice with Functions : 02/02/2023
More About Functions : 02/01/2023
Errors and Debugging : 01/31/2023
Functions : 01/30/2023
Practice with Loops and Algorithms : 01/27/2023
Algorithms : 01/26/2023
Loops : 01/25/2023
Arrays : 01/24/2023
Compound Conditionals : 01/23/2023
Conditional Expressions and Statements : 01/20/2023
Operations on Variables : 01/19/2023
Variables and Types : 01/18/2023
Welcome to CS 124 : 01/17/2023
Constructors
public class Dimensions {
double width;
double height;
Dimensions(double setWidth, double setHeight) {
width = setWidth;
height = setHeight;
}
double area() {
return width * height;
}
}
Dimensions room = new Dimensions(8.8, 10.0);
System.out.println(room.area());
Let’s continue our discussion of Java objects.
Remember that bit of syntax that looked like a method call when we create a new
Java object?
Well, it was!
Next we’ll talk about what it does.
Previously when we created instances of our new object class
es, we used new
followed by something that looked like a method call to a function accepting no parameters:
class Person {
String name;
double age;
}
Person geoff = new Person();
geoff.name = "Geoff";
geoff.age = 41.05;
It turns out that this is exactly what follows new
.
Usually when we create a new object we want to set the fields on it right away.
Rather than doing this in the fairly clumsy way shown above, Java provides a better alternative.
Let’s look at it together!
class Person {
String name;
double age;
}
Person geoff = new Person();
geoff.name = "Geoff";
geoff.age = 41.05;
There are a few things to keep in mind about constructors.
First, they must have the same name as the class
and cannot declare a return value:
class Person {
String name;
double age;
Person(String setName, double setAge) {
name = setName;
age = setAge;
}
}
Person you = new Person("Great Student", 18);
System.out.println(you.name + " is " + you.age);
You can, however, use return
in a constructor if you want to skip some parts of the initialization in certain cases:
class Person {
String name;
double age;
Person(String setName, double setAge) {
if (setAge < 0.0) {
return;
}
name = setName;
age = setAge;
}
}
Person unborn = new Person("New Baby", -0.2);
System.out.println(unborn.name);
Finally, we don’t need to declare a constructor.
If we don’t, Java will include a default constructor that takes no arguments.
Let’s see how that works:
class Person {
String name;
double age;
}
Person geoff = new Person();
geoff.name = "Geoff";
geoff.age = 41.05;
Just like with other methods, class
es can provide multiple constructors as long as they accept different parameters:
class Person {
String name;
double age;
Person(String setName, double setAge) {
name = setName;
age = setAge;
}
Person(String setName) {
name = setName;
age = 0.0;
}
}
Person geoff = new Person("Geoff", 41.05);
Person lily = new Person("Lily");
System.out.println(geoff.age);
System.out.println(lily.age);
More Practice
Need more practice? Head over to the practice page.