This Keyword in Java: Meaning & Use

Instructor: David Gloag

David has over 40 years of industry experience in software development and information technology and a bachelor of computer science

In this lesson, we'll take a look at the keyword 'this' in Java, what it is, what it's used for, and an example. At the end, you should have a good understanding of this special keyword.

The Downside of Flexibility

Java is a flexible language. Just ask any Java programmer. It's so flexible in fact, that it can be used for many things. Cell phones, tablets, and more recently, to access various features on the Internet, immediately come to mind. This is one of the things that make it so popular. But with this flexibility, also comes the possibility that one feature might interfere with another. It doesn't happen often, but there are times when it can't be avoided. It is at times like these, that Java employs a unique solution. And one of those is the keyword 'this'.

What is the Keyword 'this' in Java?

The keyword this in Java is a special keyword that represents the object you are currently executing within. The keyword may be invoked from within a constructor or method, and is simply prepended to the variable in question using the period (.) operator. An example can be seen in the code below. You don't see this keyword used all that often, as most programs are designed to avoid the situations that would require it. Still, there are a couple of places where it is necessary, which we'll take a look at in the following sections.

What is the 'this' Keyword Used For?

The keyword this provides access to instance variables. That is, variables declared at the class level. Now, you might be asking yourself, wouldn't you just use the name of the variable to access an instance variable? Most of the time, you'd be right. But consider the situation where you are within a method, and there is a local variable declared with the same name as an instance variable. When you use just the name, which one do you get? The answer is the local variable. Local variables take priority over instance variables. The local variable, in effect, hides the instance variable. In this situation, you can use the 'this' keyword to access the instance variable.

What are Some Examples of the Keyword's Use?

Okay, now that we've looked at the definition, let's look at some code. Consider the following:


class Example {
  int value = 1;
  public static void main(String args[]) {
   Example obj = new Example();
   obj.method(10);
   obj.method();
  }
  void method(int value) {
   value = 2;
   System.out.println("Instance: " + this.value)
   System.out.println("Local: " + value);
  }
}


To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support