OOP 3. Constructor - Student Class
- To introduce the concept of constructors.
- Purpose of constructors is both guaranteed initialization and convenience.
- Constructor syntax - like a method but with class name and implicit type/value.
Add a constructor for better initialization
Avoid bad initializations. One problem with the Student1 class is that the user has to explicitly initialize all fields. This requires extra typing, but more importantly, it is error-prone. If we forget to initialize a field, the default value could have bad consequences.
Convenience. Defining a constructor makes creation of an object easier to write.
Constructor Syntax
Similar to method. A constructor is similar to a method -- it's called like a method, has parameters like a method, and it returns. But it must have the same name as the class for which it is a constructor. Also, the type and return value are implicit.
Student2 example with a constructor
Here is the Student1 class with a constructor which simply initializes the fields (instance variables) from its parameters. Often constructors do more work in initialization, including checking for legal values, but for now we'll just simply copy the parameter values.
// File : oop/dataclass/Student2.java
// Purpose: Information about a student. Defines constructor.
public class Student2 {
public String firstName; // First name
public String lastName; // Last name
public int id; // Student id
//======================================== constructor
public Student2(String fn, String ln, int idnum) {
firstName = fn;
lastName = ln;
id = idnum;
}
}
Note that a constructor has no return type and the name is the same as the class name.
Using the constructor
Here is the first program rewritten to use the above class definition with a constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// File : oop/dataclass/TestStudent2.java // Purpose: Tests Student2 constructor. import javax.swing.*; public class TestStudent2 { public static void main(String[] args) { Student2 tatiana; Student2 pupil; //... Create new Student2 object with new. tatiana = new Student2("Tatiana", "Johnson", 9950842); //... Create another Student2 object. String first = JOptionPane.showInputDialog(null, "First name"); String last = JOptionPane.showInputDialog(null, "Last name"); int studID= Integer.parseInt(JOptionPane.showInputDialog(null, "ID")); pupil = new Student2(first, last, studID); JOptionPane.showMessageDialog(null, "One student is named: " + tatiana.lastName + ", " + tatiana.firstName + "\n and another is named: " + pupil.lastName + ", " + pupil.firstName); } } |
When you define a constructor, the Java compiler doesn't create a default constructor
if you define a constructor in a class, the Java compiler no longer automatically creates a default (parameterless) constructor. All object creation therefore must use your explicitly defined constructor. For example,
Student2 someone;
someone = new Student2(); // ILLEGAL. There is no default constructor.
someone = new Student2("Michael", "Maus", 1); // OK. Must specify 3 values.
The constructor can check that all fields are defined with legal values. We're not going to extend the Student2 class any further to test for legal values, but we will in the next example.
For the moment we'll leave the Student class, and move to something different to show the same ideas.