简体   繁体   中英

Using multiple constructors with one object in Java

I have two constructors for Student and am trying to use both of them with one object. But I should be doing it wrong because my output is not what I expect it to be.

Output: School: null Grade Level: 0 Intended Major: null

Student's ID number is: 154324 Student's name: Sam Bay Student's GPA: 3.56

Code for class definition:

public class Student
{
   private int id, gradeLevel;
   private String name, school, major;
   private double gpa;


   //constructor to initialize the instance variables of student object
   public Student(int id, String name, double gpa)
   {
      this.id = id;
      this.name = name;
      this.gpa = gpa;
   }

   public Student(int gradeLevel, String school, String major)
   {
      this.gradeLevel = gradeLevel;
      this.school = school;
      this.major = major;
   }    

   //toString() to display the attributions of the student object
   public String toString()
   {
      return "School: " + school +
             "\nGrade Level: " + gradeLevel +
             "\nIntended Major: " + major + "\n" +
             "\nStudent's ID number is: " + id +
             "\nStudent's name: " + name +
             "\nStudent's GPA: " + gpa;
   }

}//end class             

code for main:

public class StudentDrive

    {
       public static void main(String [] args)
       {
      //creating student objects
      Student sam = new Student(12, "Alpha High School", "Biology");
      sam = new Student(154324, "Sam Bay", 3.56);

      System.out.println(sam);

   }
}   

It seems like I've initialized the first part but I get null and 0??!!!

You can't use two constructors simultaneously on a single object.

In your code:

Student sam = new Student(12, "Alpha High School", "Biology");

creates a new Student object and assigns its reference to the variable sam .

On your next line:

sam = new Student(154324, "Sam Bay", 3.56);

This creates another Student object, separate from the first, and reassigns sam to refer to it instead. In the process, you end up orphaning the original Student and leave it open to garbage collection.

What you really want to do is either pass all data required for by a Student through a single constructor, or provide getters/setters (eg setGradeLevel(int level) ) and a layer of exceptions that prevent methods from accessing a Student object until all fields are filled. The first option is generally more sound.

For example, a complete constructor would look something like this (formatted for readability):

public Student(int id, int gradeLevel, String name, 
               String school, String major, double gpa)
{
    // fill your fields in here
}

I think you should read through the docs for a constructor again ;)

With Student sam = new Student(12, "Oakton High School", "Biology"); you are creating a Student-object with the given parameters and storing it in the variable sam.

When you call sam = new Student(154324, "Sam Bay", 3.56); you are again creating a new Student-object and storing it in sam. You are not modifying the first object but rather discarding it and creating a new one.

You should try adding a method to your Student object like:

public void setAdditionalValues(int id, String name, double gpa){
    this.id = id;
    this.name = name;
    this.gpa = gpa;
}

Hope this is helpful :)

EDIT: as mentioned earlier you could also use one constructor that takes all the arguments or implement setters for each attribute of the Student-object like this:

public void setName(String name){
    this.name = name;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM