Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of "this" keyword in java [duplicate]

Tags:

java

I was studying method overriding in Java when ai came across the this keyword. After searching much about this on the Internet and other sources, I concluded that thethis keyword is used when the name of an instance variables is same to the constructor function parameters. Am I right or wrong?

like image 450
Junior Bill gates Avatar asked Jan 03 '12 05:01

Junior Bill gates


People also ask

What is use of this keyword in Java?

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).

Is the this keyword necessary?

Accessing member variables or functions There are situations when the name of a variable, which is a member of a class, is the same as that of the parameter. In such a case, using the this keyword is mandatory with the variable which is a member of the class; otherwise, the program won't compile.

Should I always use this keyword Java?

In answer to "Are there any cases where you should always use this ?" You should use it when it is needed to avoid ambiguity, for example if there is another variable with the same name in scope.

What is not the use of this keyword in Java?

The correct answer to the question “What is not the use of 'this' keyword in Java” is, option (d). Passing itself to the method of the same class. This is one of the most important keywords in Java and is used to distinguish between local variables and variables that are passed in the methods as parameters.


2 Answers

this is an alias or a name for the current instance inside the instance. It is useful for disambiguating instance variables from locals (including parameters), but it can be used by itself to simply refer to member variables and methods, invoke other constructor overloads, or simply to refer to the instance. Some examples of applicable uses (not exhaustive):

class Foo
{
     private int bar; 

     public Foo() {
          this(42); // invoke parameterized constructor
     }

     public Foo(int bar) {
         this.bar = bar; // disambiguate 
     }

     public void frob() {
          this.baz(); // used "just because"
     }

     private void baz() {
          System.out.println("whatever");
     }

}
like image 72
Anthony Pegram Avatar answered Oct 18 '22 02:10

Anthony Pegram


this keyword can be used for (It cannot be used with static methods):

  1. To get reference of an object through which that method is called within it(instance method).
  2. To avoid field shadowed by a method or constructor parameter.
  3. To invoke constructor of same class.
  4. In case of method overridden, this is used to invoke method of current class.
  5. To make reference to an inner class. e.g ClassName.this
  6. To create an object of inner class e.g enclosingObjectReference.new EnclosedClass
like image 10
KV Prajapati Avatar answered Oct 18 '22 01:10

KV Prajapati