I have been programming for a while, but I've only made games and less serious stuff. I want to get more serious. I would like some feedback on this class that defines a person. Structure, readability, use of static variables, etc. Please point out everything that can be improved.
public class Person {
private String firstName, lastName, address;
private int age;
private boolean hasAddress = false;
private boolean hasAge = false;
private static final String NO_ADDRESS = "No address defined";
//Constructors
//Minimum information required
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
//All possible info
public Person(String firstName, String lastName, int age, String address) {
this(firstName, lastName);
this.age = age;
this.address = address;
hasAge = true;
hasAddress = true;
}
//No address defined
public Person(String firstName, String lastName, int age) {
this(firstName, lastName);
this.age = age;
hasAge = true;
}
//No age defined
public Person(String firstName, String lastName, String address) {
this(firstName, lastName);
this.address = address;
hasAddress = true;
}
//Methods
//Get-methods
public String getName() {
return firstName + lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
if (hasAge)
return age;
else
return 0; //Return 0 if no age is defined
}
public String getAddress() {
if (hasAddress)
return address;
else
return NO_ADDRESS;
}
//Set-methods
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setAge(int age) {
this.age = age;
}
public void setAddress(String address) {
this.address = address;
}
}
getName()just squashes together the first name and the last name, without a space in-between. This is probably not what you intended. \$\endgroup\$getAge()method that calculates the age in runtime and returns it. \$\endgroup\$