Skip to main content
added note , one comment in the code, delete one line
Source Link
dariosicily
  • 4.1k
  • 1
  • 11
  • 22

Note : I used Integer for age instead of int to use OptionalInt.

In this way the user of your class Person is forced to check if the fields address and age are set before using them:

Note : I used Integer for age instead of int to use OptionalInt.

In this way the user of your class Person is forced to check if the fields address and age are set before using them:

In this way the user of your class Person is forced to check if the fields address and age are set before using them:

added note , one comment in the code
Source Link
dariosicily
  • 4.1k
  • 1
  • 11
  • 22
public class Person {
    private String firstName;
    private String lastName; 
    private String address;
    private Integer age; //<-- better use int age, see note for details
    
    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public Optional<String> getAddress() {
        return Optional.ofNullable(address);
    }

    public OptionalInt getAge() {
        return OptionalInt.of(age);
    }

   //others setters and getters omitted for brevity
}

Note: following @Nathan's comment below, instead of use Integer for age field it is better to use directly int to avoid boxing and unboxing operations.

public class Person {
    private String firstName;
    private String lastName; 
    private String address;
    private Integer age;
    
    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public Optional<String> getAddress() {
        return Optional.ofNullable(address);
    }

    public OptionalInt getAge() {
        return OptionalInt.of(age);
    }

   //others setters and getters omitted for brevity
}
public class Person {
    private String firstName;
    private String lastName; 
    private String address;
    private Integer age; //<-- better use int age, see note for details
    
    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public Optional<String> getAddress() {
        return Optional.ofNullable(address);
    }

    public OptionalInt getAge() {
        return OptionalInt.of(age);
    }

   //others setters and getters omitted for brevity
}

Note: following @Nathan's comment below, instead of use Integer for age field it is better to use directly int to avoid boxing and unboxing operations.

Source Link
dariosicily
  • 4.1k
  • 1
  • 11
  • 22

I'm adding just some thoughts to explain how I would solve the problem: assuming in the simplest scenario you have two fields firstName and lastName that must be always compiled when you create a Person object then the simplest way is have to have a constructor that takes them as parameters:

public class Person {
    private String firstName;
    private String lastName; 
    
    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName  = lastName;
    }
    //getters and setters omitted for brevity
}

After you have two fields address and age that in one or more moments of the life of your objects can be null, to signal this situation to the user of your Person class you chose to return the values "No address defined" and 0 like the code below:

private static final String NO_ADDRESS = "No address defined"; 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; }

I disagree with your choice because the user of your class Person is obliged to know the value of String "No address defined" to identify which address is valid and which not with String.equals method and for me 0 is a valid value for age. My possible solution is the use of Optional and OptionalInt for address and age like my code below:

public class Person {
    private String firstName;
    private String lastName; 
    private String address;
    private Integer age;
    
    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public Optional<String> getAddress() {
        return Optional.ofNullable(address);
    }

    public OptionalInt getAge() {
        return OptionalInt.of(age);
    }

   //others setters and getters omitted for brevity
}

Note : I used Integer for age instead of int to use OptionalInt.

In this way the user of your class Person is forced to check if the fields address and age are set before using them:

Person person = new Person("firstName", "lastName");
person.setAddress("address");
person.setAge(10);
Optional<String> optAddress = person.getAddress();
if (optAddress.isPresent()) {
    System.out.println(optAddress.get());
}
OptionalInt optAge = person.getAge();
if (optAge.isPresent()) {
    System.out.println(optAge.getAsInt());
}