There is a coding style that I've seen a lot in Android Programming when using Google Services libraries where they use dots to call methods after initializing an instance of a class.
For example, lets say I have a Person class:
public class Person {
private String firstName;
private String lastName;
private String occupation;
private String primarySkill;
private String secondarySkill;
/* ******************
* CONSTRUCTOR
* ******************/
public Person(String firstName, String lastName) {
setFirstName(firstName);
setLastName(lastName);
}
/* ******************
* MUTATORS
* ******************/
// firstName Setter:
public void setFirstName(String firstName) {
this.firstName = firstName;
}
// lastName Setter:
public void setLastName(String lastName) {
this.lastName = lastName;
}
// occupation Adder:
public void addOccupation(String occupation) {
this.occupation = occupation;
}
// primarySkill Adder:
public void addPrimarySkill(String primarySkill) {
this.primarySkill = primarySkill;
}
// secondarySkill Adder:
public void addSecondarySkill(String secondarySkill) {
this.secondarySkill = secondarySkill;
}
}
Now, when I create an instance of this class, I want to be able to do the following:
Person bob = new Person("Bob", "Anderson")
.addOccupation("Student")
.addPrimarySkill("Java")
.addSecondarySkill("SQL");
However this gives me syntax errors. How can I build a class that can allow me to do this?