简体   繁体   中英

Java class setter not setting correct value to class field.

I have a simple java program I and I cannot figure out why the setter for a class will not set the correct value.

I have a class Employee, a class Department, a class Company. Once I am able to set correct values to the fields of an Employee instance I will then store that employee in a arraylist of employees in an instance of Department(arrayList field).

The class called Employee. It has four fields, String fName, String lName, int age, String department. I am able to set fName and lName though age is always set to 0 and department is always set to null.

Here is the code for the employee class:

public class Employee {
private String fName;
private String lName;
private String department;
private int age;

//getters and setters for the private fields of the Employee class
public void setAge(int num){
    num = age; 
}

public int getAge(){
    return age;
}

public void setDepartment(String dep){
    dep = department; 
}

public String getDepartment(){
    return department;
}

public void setfName(String afName){
    fName = afName;
}

public String getfName(){
    return fName;
}

public void setlName(String alName){
    lName = alName;
}

public String getlName(){
    return lName;
}
}

Here is the code for a method called addEmployee:

public void AddEmployee(Department depInstance){
    String firstName = JOptionPane.showInputDialog("Enter employee First name");
    String lastName = JOptionPane.showInputDialog("Enter employee last name");
    int empAge = Integer.parseInt(JOptionPane.showInputDialog("Enter employee age"));
    String empDep = JOptionPane.showInputDialog("Enter employee department");

    Employee employeeToAdd = new Employee();
    employeeToAdd.setfName(firstName);
    employeeToAdd.setlName(lastName);
    employeeToAdd.setAge(empAge);
    employeeToAdd.setDepartment(empDep);

    //test input and variable setting
    System.out.println("--------Inputs------");
    varTester(firstName,lastName,empAge,empDep);
    System.out.println("--------Recorded Vals------");
    varTester(employeeToAdd.getfName(), employeeToAdd.getlName(),employeeToAdd.getAge(),employeeToAdd.getDepartment());

public static void varTester(String empfName, String emplName, int empAge, String empDep){
    System.out.println(empfName);
    System.out.println(emplName);
    System.out.println(empAge);
    System.out.println(empDep);
}

}

This is the output from the test method varTester():

--------Inputs------
Somefirstname
Somelastname
32
Accounting
--------Recorded Vals------
Somefirstname
Somelastname
0
null

I test the values received from the showInputDialog's and it is the correct values I want to store int the class instance fields of employeeToAdd though only the first and last name values are set and not the age or department. Can someone point me in the right direction. Thank you.

You got the setter backwards. It should be :

public void setAge(int num){
    age = num; 
}

You have the same error in setDepartment .

You are supposed to assign to the member variable, not to the argument of the setter method.

Your setter sets the argument not the private field.

public void setAge(int num){
    num = age; 
}

public void setDepartment(String dep){
    dep = department; 
}

Change it to:

public void setAge(int num){
   age = num; 
}

public void setDepartment(String dep){
   department = dep; 
}

It should be:

public void setAge(int num){
    age = num; 
}
public void setDepartment(String dep){
    department = dep; 
}

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