Initializer Lists and Redirecting
Set finals and reuse constructors.
Setting final Fields
A final field must be set before the constructor body runs. The initializer list is the perfect place to give it its one value.
class Circle {
final double area;
Circle(double r) : area = 3.14 * r * r;
}The Initializer List Syntax
After the parameter list, add a colon and comma-separated assignments. This initializer list runs before the constructor body.
Point(int x, int y) : x = x, y = y;All lessons in this course
- Default and Parameterized Constructors
- Named Constructors for Clear Intent
- Initializer Lists and Redirecting
- Factory and const Constructors