bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java this Keyword

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java this Keyword?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Order

Put the learning moves in the order that makes the concept easiest to apply.

The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter).
The this keyword refers to the current object in a method or constructor.
Definition and Usage

❮ Java Keywords

this

Definition and Usage

The this keyword refers to the current object in a method or constructor.

The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter). If you omit the keyword in the example above, the output would be "0" instead of "5".

this can also be used to:

  • Invoke current class constructor
  • Invoke current class method
  • Return the current class object
  • Pass an argument in the method call
  • Pass an argument in the constructor call

Related Pages

Read more about objects in our Java Classes/Objects Tutorial .

Read more about constructors in our Java Constructors Tutorial .

Read more about methods in our Java Methods Tutorial .

❮ Java Keywords

Previous

Java synchronized Keyword

Next

Java throw Keyword