Reputation: 17
I am learning java and in class we started working with multiple classes.... I have an employee class with my default constructor. In it I have the variables with default values. The problem that I have is that the values of the variables in the default constructor are not used so I am getting yellow underlines under the variables. Can someone steer me in the right direction.
package week2;
import java.text.NumberFormat;
public class Employee {
//Instance Variables
private String firstName = "", lastName = "";
private char gender = 'U';
private int dependents = 0;
private double annualSalary = 0;
//Default Employee Constructor
public Employee() {
String firstName = "not given";
String lastName = "not given";
char gender = 'U';
int dependents = 0;
double annualSalary = 20000;
} //End of default Employee Constructor
//Employee Constructor with variables
public Employee(String firstName, String lastName, char gender, int dependents, double salary) {
this.firstName = firstName;
this.lastName = lastName;
this.gender = gender;
this.dependents = dependents;
this.annualSalary = salary;
} //End of Overloaded Employee Constructor
//Calculate Pay
public double annualSalary() {
return (getAnnualSalary() / 52);
} //End Calculate Pay
//Display Employee
public void displayEmployee() {
NumberFormat nf = NumberFormat.getCurrencyInstance();
System.out.println("First Name: \t" + this.firstName);
System.out.println("Last Name: \t" + this.lastName);
System.out.println("Gender: \t" + this.gender);
System.out.println("Dependents: \t" + this.dependents);
System.out.println("Annual Salary: \t" + nf.format(this.annualSalary));
System.out.println("Weekly Pay: \t" + nf.format(annualSalary()));
} //End Display Employee
//Getters and Setters
//Get-Set firstName
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
//Get-Set lastName
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
//Get-Set Gender
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
//Get-Set dependents
public int getDependents() {
return dependents;
}
public void setDependents(int dependents) {
this.dependents = dependents;
}
//Get-Set annualSalary
public double getAnnualSalary() {
return annualSalary;
}
public void setAnnualSalary(double annualSalary) {
this.annualSalary = annualSalary;
}
}
Upvotes: 0
Views: 1718
Reputation: 4692
You are creating local variables
in your default constructor. Remove the declaration and it will assign value to class level variables.
//Default Employee Constructor
public Employee() {
firstName = "not given";
lastName = "not given";
gender = 'U';
dependents = 0;
annualSalary = 20000;
} //End of default Employee Constructor
Note: Yellow color indicate a warning. In your case it is giving hint that you are creating local variables and assigning values to it, which is not used in your constructor and local variables are not visible outside the constructor.
You can use this.firstName
or firstName
if no other local variable is defined with the same name. (You can use these both in default constructor in your case)
If some local variable is defined with the same name then firstName
refers to local variable and this.firstName
refers to class level variable.(you can see that in your parametrized constructor)
Upvotes: 3
Reputation: 3037
Here the concept you have to be clear is the scope of variables, In your default constructor
//Default Employee Constructor
public Employee() {
String firstName = "not given";
String lastName = "not given";
char gender = 'U';
int dependents = 0;
double annualSalary = 20000;
} //End of default Employee Constructor
The variables declared have scope only within this constructor, they are different from the instance variables declared at the start of the class. Hence, these variables are not used anywhere, so the compiler warning.
Upvotes: 0
Reputation: 85799
The no-arg constructor declares new variables and they are initialized but not used, as the compiler says. Note that these variables are not the fields in the class.
public Employee() {
//this is a local variable in the method
String firstName = "not given";
//this variable is not the same as this:
this.firstName = "not given";
//same for other variables
}
There are two solutions for this case:
Remove all the declarations of local variables and use the fields instead:
public Employee() {
this.firstName = "not given";
//...
}
Use this()
and call the other constructor with the initial values of your fields:
public Employee() {
this("not given", "not given", 'U', 0, 2000);
}
Upvotes: 2