3 · Object-Oriented Core · lesson 13 of 41
Constructors
How new objects are initialized.
Java
public class Point {
final double x, y;
public Point(double x, double y) { // constructor
this.x = x;
this.y = y;
}
public Point() { this(0, 0); } // chained constructor
}Key takeaways
- ✓Constructors have no return type and share the class name.
- ✓this(...) chains to another constructor in the same class.