2

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??!!!

PTheCoolGuy
  • 63
  • 1
  • 7
  • 1
    The info you enter in the first object creation is lost when you create a completely new second object. My question is why would you want to do it like this? – takendarkk Nov 08 '14 at 23:44
  • You're only ever going to initialize half of the variables you want, anyway. There's a slightly better approach to do this, but it depends on which way you want to go; are you comfortable with one giant constructor, or would you be more open to a builder? – Makoto Nov 08 '14 at 23:45
  • 1
    See [this thread](http://stackoverflow.com/questions/18999635/how-to-execute-multiple-constructor-when-creating-single-object) for the same question. – mattias Nov 08 '14 at 23:46

2 Answers2

4

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 (e.g. 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
}
yossarian
  • 1,537
  • 14
  • 21
0

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;
}
Gelx
  • 129
  • 7