Skip to content
Advertisement

Variable birthDate might not have been initialized…. but I thought I did?

Here is the code:

public abstract class Employee {
  private final String firstName;
  private final String lastName;
  private final String socialSecurityNumber;
  private final String birthDate;

//constructor
public Employee(String firstName, String lastName, 
                String socialSecurityNumber){
    
    this.firstName = firstName;
    this.lastName = lastName;
    this.socialSecurityNumber = socialSecurityNumber;
    this.birthDate = birthDate;//error is here
    
}

//return first name
public String getFirstName(){
    return firstName;
}
//return last name
public String getLastName(){
    return lastName;
}
//return social security number
public String getSocialSecurityNumber(){
    return socialSecurityNumber;
}
//return birthdate
public String getBirthDate(){
    return birthDate;
}
//return String representation of Employee object
@Override
public String toString(){
    return String.format("%s%s%nsocial security number: %s" , 
                         getFirstName(), getLastName(), 
                         getSocialSecurityNumber(), getBirthDate());
}
//abstract method must be overridden by concrete subclasses
public abstract double earnings();
//no implementation here
}

I keep getting the error “variable birthDate might not be initialized”, but the provided solution simply adds “this.birthDate = birthDate;”, which doesn’t work (I already added that). What am I missing? Shouldn’t this.birthDate = birthDate initialize birthDate, the way this.lastName = lastName and the others do?

My assignment is to add private instance variable birthDate in class Employee (and use class Date from a different figure to represent an employee’s birthday). I’ll include class Date as well, in case that might be part of the reason for the error…. but I would think not because firstName and lastName etc. doesn’t give that error?

public class Date {
   private int month; // 1-12
   private int day; // 1-31 based on month
   private int year; // any year

   private static final int[] daysPerMonth =
     { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

  // constructor: confirm proper value for month and day     given the year
  public Date(int month, int day, int year) {
      
  }
  public void setMonth(int Month){
     // check if month in range
     if (month <= 0 || month > 12)
        throw new IllegalArgumentException(
           "month (" + month + ") must be 1-12");
     else{
         month=Month;
     }
     }
  public int getMonth(){
      return month;
  }//display month
  
public void setDay(int Day){
     // check if day in range for month
     if (day <= 0 ||
        (day > daysPerMonth[month] && !(month == 2 && day == 29)))
        throw new IllegalArgumentException("day (" + day +
           ") out-of-range for the specified month and year");
else{
 day = Day;
}
}
public int getDay(){
return day;//display day
}

public void setYear(int Year){
  {// check for leap year if month is 2 and day is 29
     if (month == 2 && day == 29 && !(year % 400 == 0 ||
           (year % 4 == 0 && year % 100 != 0)))
        throw new IllegalArgumentException("day (" + day +
           ") out-of-range for the specified month and year");
     else{
         year = Year;
     }
  }
}
     public int getYear(){
     return year;
     }
  {
     this.month = month;
     this.day = day;
     this.year = year;

     System.out.printf("Date object constructor for date %s%n", this);
  }

  // return a String of the form month/day/year
  public String toString() {
     return String.format("%d/%d/%d", month, day, year);
  }
}

Advertisement

Answer

public class Person {
  String firstName;
  String lastName; // instance variables

  public Person(String firstName, String lastName) { // parameters to constructor
    this.firstName = firstName; // this.firstName is the instance variable, value you assign is the parameter
    this.lastName = lastName;
  }

}

This will work. But, let’s say you do this:

public Person(String firstName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

In this constructor, there is no parameter or local variable called lastName, so it’ll take the instance variable, which isn’t instantiated.

You’ll need to add the parameter for birthDate to your constructor

EDIT in your case, this:

//constructor
public Employee(String firstName, String lastName, 
                String socialSecurityNumber){
    
    this.firstName = firstName;
    this.lastName = lastName;
    this.socialSecurityNumber = socialSecurityNumber;
    this.birthDate = birthDate;//error is here
    
}

should become:

//constructor
public Employee(String firstName, String lastName, 
                String socialSecurityNumber, String birthDate){
    
    this.firstName = firstName;
    this.lastName = lastName;
    this.socialSecurityNumber = socialSecurityNumber;
    this.birthDate = birthDate;//error solved, it's taken the parameter
    
}

or, perhaps:

//constructor
public Employee(String firstName, String lastName, 
                String socialSecurityNumber){
    
    this.firstName = firstName;
    this.lastName = lastName;
    this.socialSecurityNumber = socialSecurityNumber;
    this.birthDate = ""; // or just leave this line completely, if you don't know the birthDate
    
}

Keep in mind, if you add a parameter, you’ll need to update the calls to the constructor as well.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement