Creating a Java Student Class with Constructor Chaining
Chaining Constructors
Write a Java program to create a class called Student with instance variables studentId, studentName, and grade. Implement a default constructor and a parameterized constructor that takes all three instance variables. Use constructor chaining to initialize the variables. Print the values of the variables.
Sample Solution:
Java Code:
Student.java
// Define the Student class
public class Student {
// Private instance variables
private int studentId;
private String studentName;
private String grade;
// Default constructor
public Student() {
// Call the parameterized constructor with default values
this(0, "Unknown", "None");
}
// Parameterized constructor
public Student(int studentId, String studentName, String grade) {
// Initialize studentId with the provided parameter
this.studentId = studentId;
// Initialize studentName with the provided parameter
this.studentName = studentName;
// Initialize grade with the provided parameter
this.grade = grade;
}
// Main method to test the Student class
public static void main(String[] args) {
// Create a new Student object using the default constructor
Student student1 = new Student();
// Print the values of the instance variables for student1
System.out.println("Student1 ID: " + student1.studentId);
System.out.println("Student1 Name: " + student1.studentName);
System.out.println("Student1 Grade: " + student1.grade);
// Create a new Student object using the parameterized constructor
Student student2 = new Student(101, "Cullen", "A");
// Print the values of the instance variables for student2
System.out.println("Student2 ID: " + student2.studentId);
System.out.println("Student2 Name: " + student2.studentName);
System.out.println("Student2 Grade: " + student2.grade);
}
}
Output:
Student1 ID: 0 Student1 Name: Unknown Student1 Grade: None Student2 ID: 101 Student2 Name: Cullen Student2 Grade: A
Explanation:
- Define the Student class:
- The Student class is defined with the keyword class.
- Private instance variables:
- The Student class has three private instance variables: studentId (an int), studentName (a String), and grade (a String).
- Default constructor:
- The default constructor Student() is defined.
- Inside the default constructor, constructor chaining is used by calling the parameterized constructor this(0, "Unknown", "None").
- Parameterized constructor:
- The parameterized constructor Student(int studentId, String studentName, String grade) is defined.
- Inside the constructor, this.studentId is initialized with the provided studentId parameter.
- this.studentName is initialized with the provided studentName parameter.
- this.grade is initialized with the provided grade parameter.
- Main method:
- The main method is defined to test the Student class.
- Two Student objects (student1 and student2) are created using different constructors.
- The values of the instance variables for each Student object are printed to the console.
Note on Constructors:
In the above exercise, the constructors for the Student class work by:
- Constructor Chaining:The default constructor calls the parameterized constructor using this(0, "Unknown", "None"), ensuring that all instance variables are initialized through a single constructor.
- Initialization with Parameters: The parameterized constructor initializes the instance variables studentId, studentName, and grade with the provided values, promoting code reuse and consistency.
- Encapsulation: By keeping the instance variables private and initializing them through constructors, the class ensures controlled initialization and data integrity, encapsulating the logic for setting default values within the class itself.
Java Code Editor:
Improve this sample solution and post your code through Disqus.
Java Constructor Previous: Creating a Java Book Class with Multiple Constructors.
Java Constructor Next: Creating a Java Rectangle Class with Parameterized and Copy Constructors.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics