0

I have a created a class that creates an instance of an object

public class EmployeeProfile {
    /////////INSTANCE VARIABLES/////////
    private static String fName;
    private static String lName;
    private static String email;
    private static String phone;


    ////////CONSTRUCTORS////////////
    public EmployeeProfile()
    {

    }

    public EmployeeProfile(String firstName, String lastName, String emailAdd, String pNumber)
    {
        fName = firstName;
        lName = lastName;
        email = emailAdd;
        phone = pNumber;
    } 
}

When I call the empty constructor and populate it myself with the methods I've created everything is fine. However when I call a new object with a new name using the second constructor and the parameters, they overwrite the data from the first object!!

    EmployeeProfile prof1 = new EmployeeProfile();
    prof1.firstName("John");
    prof1.lastName("Doe");
    prof1.email("[email protected]");
    prof1.phone("555-555-5555");


    EmployeeProfile prof2 = new EmployeeProfile("Jane", "Doe", "[email protected]", "555-123-4567");

    System.out.println(prof1.getProfile());
    System.out.println(prof2.getProfile());

When I run this prof1 and prof2 both return the data from prof2. What am I doing wrong here?

2
  • 1
    What do you think static means? Commented Oct 12, 2014 at 2:57
  • geez!! lol I guess I should have mentioned I'm fairly new to java as well. Thanks! Commented Oct 12, 2014 at 2:59

4 Answers 4

2
/////////INSTANCE VARIABLES/////////
private static String fName;
private static String lName;
private static String email;
private static String phone;

the comment and code are inconsistent. static signifies class variables, not instance. Therefore, they're shared between class instances.

Sign up to request clarification or add additional context in comments.

Comments

2

you make static variables.

private static String fName;
private static String lName;
private static String email;
private static String phone;

when you create some variable static that value overwrite every time. so, please change it to, without static keyword.

e.g :private String fName;

2 Comments

thank you, I was under the impression static meant it WOULDN'T overwrite every time. problem solved
Static means that every instance of a class will share the same instance of the attribute/variable
1

static means that the variable is static throughout the class. Remove static so you variables look like this:

private String fName;
private String lName;
private String email;
private String phone;

Good luck.

Comments

0

The problem is that Static attributes are shared among the instances of that class, that is why the attributes are being overwritten constantly. Thus, you should delete the Static descriptor in the attributes. Besides, in the Object Oriented Programming Paradigm is always recommended to access an Object's attribute through it's getters & setters. With that changes, the code will be the following:

public class EmployeeProfile {
    /////////INSTANCE VARIABLES/////////
    private String fName;
    private String lName;
    private String email;
    private String phone;


    ////////CONSTRUCTORS////////////
    public EmployeeProfile()
    {

    }

    public EmployeeProfile(String firstName, String lastName, String emailAdd, String pNumber)
    {
        this.setfName(firstName);
        this.setlName(lastName);
        this.setEmail(emailAdd);
        this.setPhone(pNumber);
    }

    public String getfName() {
        return fName;
    }

    public void setfName(String fName) {
        this.fName = fName;
    }

    public String getlName() {
        return lName;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    } 

}

Hope it helps.

Clemencio Morales Lucas.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.