this Keyword

In this chapter you will learn:

  1. How to use this keyword to reference class member variables
  2. How to reference class member variables
  3. How to create Person class

this keyword and class member variables

this refers to the current object. The following code shows how to use this keyword.

// A use of this. 
Rectangle(double w, double h) { 
    this.width = w; // this is used here
    this.height = h; 
}

Hidden instance variables and this

Use this to reference the hidden instance variables

Member variables and method parameters may have the same name. Under this situation we can use this to reference the member variables.

Rectangle(double width, double height) { 
    this.width = width; 
    this.height = height; 
}

A demo class

The following code defines a Person class. It has three member variables to store information about person's name, title and address. Then several instances are created

class Person{/* j av  a  2  s  .c om*/
    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:

Next chapter...

What you will learn in the next chapter:

  1. How to declare a static method and use it
  2. How to declare static variables and use them
  3. How to use static initialization block
  4. A demo for static variables and static methods