- Design robust classes - Don't rely on the programmer to use the class correctly - Constructor - Special method that is executed when a new class is created via the new method - Must have the same name as its class - Syntax public ( ) { } - No return type - Modifier - Need not be public - Anything else - Extremely rare - Not covered - Default constructor - If no constructor defined, automatically provided by compiler - If do define own, no default is added - Can define multiple constructors for the same class - Lets programmer create a new instance of the class in different ways - Conditions for multiple constructors: 1. Different number of parameters OR 2. Different combination of types among parameters - Different parameter names still invalid - Constructor purpose: initialize object to valid state - Do so by intializing all member data in the constructor - Define multiple constructors more cleanly - Have them call each other - Substitute reserved word this for the class name in the constructor being called - Recommend have the calling line be the only line in the constructor - At the very least, should be the first line of the constructor Quick Check 1. ClassA is invalid because it has a return type ClassC is invalid because, not only does it have a return type, it is also lacking a visibility modifier 2. The constructor with parameter int X conflicts with the constructor with parameter int Y because they have the same number and type of parameters. 3. class Student { private String name; public Student() { this(""); } public Student(String initName) { name = initName; } public void assign(String newName) { name = newName; } public String identify() { return name; } }