Reputation: 79
Hello I was wondering how I can make the most dynamic get set methods in a java program I am working on? The program has multiple fields and each field needs to have its own get and set method but I want only one get set method that can get and set any field in the class I want.
package kalsi;
public class ContestantInformation {
String firstName, lastName, city, province, postalCode, streetName, streetNumber, phoneNum, birthDate;
public ContestantInformation() {
}
public ContestantInformation(String firstName, String lastName, int streetNumber, String streetName, String city,
String province, String postalCode, int phoneNum, int birthDate) {
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = "" + birthDate;
this.streetNumber = "" + streetNumber;
this.streetName = streetName;
this.city = city;
this.postalCode = postalCode;
this.phoneNum = "" + phoneNum;
}
public void setName(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
Upvotes: 2
Views: 1868
Reputation: 11163
You can use your IDE to generate getter/setter
methods. If you are using eclipse then you can do it by going to Source>generate methods.
Or alternatively you can use lombok generate getter setter method dynamically. In this case you don't even need to write your getter/setter
methods. Look at the example -
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
public class Person {
@Getter @Setter private String name;
@Getter @Setter private int age = 10;
}
Upvotes: 4