Java this Keyword
In this chapter you will learn:
- What is Java this keyword for
- How to use this keyword to reference class member variables
- How to reference class member variables
- Example - use this to reference instance variable
Description
this
refers to the current object.
this
can be used inside any method to refer to the current object.
Syntax
The following code shows how to use this
keyword.
// A use of this. /*from ww w.java 2 s . co m*/
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;
}
Example
The following example shows how to use this to reference instance variable.
class Person{/*from w w w.j a v a 2 s .c om*/
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Main{
public static void main(String[] args) {
Person person = new Person("Java");
System.out.println(person.getName());
person.setName("new name");
System.out.println(person.getName());
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- How to declare a static method and use it
- Syntax for static keyword
- Restrictions for Java static keyword
- Example - static methods
- Example - static variables
- How to use static initialization block
Java Class
Java Object
Java Object Reference Variable
Java Methods
Java Method Return
Java Method Parameters
Java Class Constructors
Java Default Constructor
Java Constructor Parameters
Java Method Overload
Java Constructors Overload
Java Method Argument Passing
Java Method Recursion
Java Nested Class
Java Anonymous Classes
Java Local Classes
Java Member Classes
Java Static Member Classes
Java Class Variables
Java main() Method
Java Class Inheritance
Java super keyword
Java Method Overriding
Java Constructor in hierarchy
Polymorphism
Java final keyword
Java Abstract class
Java Class Access Control
Java Package
Java Packages Import
Java Interface
Java Interface as data type
Java interface as build block
Java instanceof operator
Java Source Files
Java Object
Java Object Reference Variable
Java Methods
Java Method Return
Java Method Parameters
Java Class Constructors
Java Default Constructor
Java Constructor Parameters
Java this Keyword
Java static keywordJava Method Overload
Java Constructors Overload
Java Method Argument Passing
Java Method Recursion
Java Nested Class
Java Anonymous Classes
Java Local Classes
Java Member Classes
Java Static Member Classes
Java Class Variables
Java main() Method
Java Class Inheritance
Java super keyword
Java Method Overriding
Java Constructor in hierarchy
Polymorphism
Java final keyword
Java Abstract class
Java Class Access Control
Java Package
Java Packages Import
Java Interface
Java Interface as data type
Java interface as build block
Java instanceof operator
Java Source Files