The Person class: another demo


class Person{
    private String name;
    private String title;
    private String address;
    /**
     * Constructor to create Person object
     */
    public Person() {
    }
    /**
     * Constructor with parameter
     *
     * @param name
     */
    public Person(String name) {
        this.name = name;
    }
    /**
     * Method to get the name of person
     *
     * @return name
     */
    public String getName() {
        return name;
    }
    /**
     * Method to set the name of person
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * Method to get the title of person
     *
     * @return title
     */
    public String getTitle() {
        return title;
    }
    /**
     * Method to set the title of person
     *
     * @param title
     */
    public void setTitle(String title) {
        this.title = title;
    }
    /**
     * Method to get address of person
     *
     * @return address
     */
    public String getAddress() {
        return address;
    }
    /**
     * Method to set the address of person
     *
     * @param address
     */
    public void setAddress(String address) {
        this.address = address;
    }
    /**
     * Method to get name with title of person
     *
     * @return nameTitle
     */
    public String getNameWithTitle() {
        String nameTitle;
        if (title != null) {
            nameTitle = name + ", " + title;
        } else {
            nameTitle = name;
        }
        return nameTitle;
    }
    /**
     * Method used to print the information of person
     */
    @Override
    public String toString() {
        return "Info [" +
                "name='" + name + '\'' +
                ", title='" + title + '\'' +
                ", address='" + address + '\'' +
                ']';
    }
}
public class Main{
    public static void main(String[] args) {
        Person person = new Person();
        person.setName("Jack");
        person.setTitle("Coder");
        person.setAddress("Main Street");
        System.out.println(person);
        String nameTitle1 = person.getNameWithTitle();
        System.out.println("Name with title: " + nameTitle1);
        Person person2 = new Person("Sarah");
        String nameTitle2 = person2.getNameWithTitle();
        System.out.println("Name with title 2: " + nameTitle2);
    }
}

The output:


Info [name='Jack', title='Coder', address='Main Street']
Name with title: Jack, Coder
Name with title 2: Sarah
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.