0

I have an object Customer with the following parameterized constructor:
Customer(String lastName, String firstName, String phone, String email).

I need to create getter and setter methods for all the parameters. I have tried the following but I am not sure if it is correct.

public String getLastName() {
    return lastName;
}

public void setLastName(String newLastName) {
    this.LastName = newLastName;
}
NotZack
  • 518
  • 2
  • 9
  • 22
John Doe
  • 9
  • 2

3 Answers3

0

Be careful with your use of uppercase/lowercase letters when addressing variables in Java!

lastName is not the same variable as LastName

That aside, your getters and setters look correct. In general, a getter is a function that takes no parameters and returns an instance variable value, and a setter is a function that takes a parameter and updates an instance variable with it.

You could, if you'd like, make your setter return a boolean value to flag whether or not the update succeeded (although it's not necessary for your current example, since we can assume that the update will succeed).

public boolean setLastName(String newLastName){
   //Attempt to update the instance variable
   this.LastName = newLastName;

   //check that the instance variable has been successfully updated. 
   //Return true if that's the case. False otherwise
   return (this.LastName).equals(newLastName);
}
0

No need to use newLastName. Just use same name for method parameters.

public String getLastName(){
    return lastName;
}

public void setLastName(String lastName){
    this.lastName = lastName;
}

If you are using Eclipse, you could In your Java File, Right Click -> Source -> Generate Getters and Setters.

Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126
0

This is a deeper question than you think. There are actually a few different styles of getter & setter, and, if you're on a team, it is more important that you are all using the same style than that you pick the "correct" style. If it's just for yourself, what you have is fine. If it is for a school project, the teacher might have a style guide. If it's for a real job, the architect SHOULD have a style guide.

For instance, some people like this style of setter

class PersonalInfo {
    private String firstname;
    private String lastname;

    public PersonalInfo firstname(String firstname) {
        this.firstname = firstname;
        return this;
    }
    public PersonalInfo lastname(String lastname) {
        this.lastname = lastname;
        return this;
    }
}

Which allows for code like

PersonalInfo pi = new PersonalInfo()
.firstname("John")
.lastname("Smith");

If you look up builder-style constructors, it's sort of a cheap way to the same result but without actually creating the separate class.

Second, if you're writing these yourself, you're making a mistake. If the editor you're using doesn't have a function to create them, then go download Eclipse and use that.

Third, I'd prefer not writing them at all, but using Lombok. It is a tool that generates all the boilerplate, repetitive stuff that clutters up your source code.

Zag
  • 638
  • 4
  • 8