What keyword in Java is the name of a reference that an object can use to refer to itself?

7

New! Save questions or answers and organize your favorite content.
Learn more.

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?

asked Jan 3, 2012 at 5:15

2

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");
     }

}

answered Jan 3, 2012 at 5:17

What keyword in Java is the name of a reference that an object can use to refer to itself?

Anthony PegramAnthony Pegram

121k26 gold badges220 silver badges245 bronze badges

2

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

answered Jan 3, 2012 at 5:24

What keyword in Java is the name of a reference that an object can use to refer to itself?

KV PrajapatiKV Prajapati

92.7k19 gold badges144 silver badges183 bronze badges

You are right, but this is only a usage scenario, not a definition. The this keyword refers to the "current object". It is mostly used so that an object can pass itself as a parameter to a method of another object.

So, for example, if there is an object called Person, and an object called PersonSaver, and you invoke Person.SaveYourself(), then Person might just do the following: PersonSaver.Save( this );

Now, it just so happens that this can also be used to disambiguate between instance data and parameters to the constructor or to methods, if they happen to be identical.

answered Jan 3, 2012 at 5:24

What keyword in Java is the name of a reference that an object can use to refer to itself?

this keyword have following uses
1.used to refer current class instance variable

 class Student{  
int id;  
String name;  

student(int id,String name){  
this.id = id;  
this.name = name;  
}  
void display(){System.out.println(id+" "+name);}  
public static void main(String args[]){  
Student s1 = new Student(111,"Karan");  
Student s2 = new Student(222,"Aryan");  
s1.display();  
s2.display();  
}  
}  

here parameter and instance variable are same that is why we are using this
2.used to invoke current class constructor

class Student{  
int id;  
String name;  
Student (){System.out.println("default constructor is invoked");}  

Student(int id,String name){  
this ();//it is used to invoked current class constructor.  
this.id = id;  
this.name = name;  
}  
void display(){System.out.println(id+" "+name);}  

public static void main(String args[]){  
Student e1 = new Student(111,"karan");  
Student e2 = new Student(222,"Aryan");  
e1.display();  
e2.display();  
}  
}    

3.this keyword can be used to invoke current class method (implicitly)

4.this can be passed argument in the method call

5.this can be passed argument in the constructor call

6.this can also be used to return the current class instance

answered Sep 12, 2014 at 12:06

This refers current object. If you have class with variables int A and a method xyz part of the class has int A, just to differentiate which 'A' you are referring, you will use this.A. This is one example case only.

public class Test
{
int a;

public void testMethod(int a)
{
this.a = a;
//Here this.a is variable 'a' of this instance. parameter 'a' is parameter.
}
}

answered Jan 3, 2012 at 5:21

kosakosa

65.3k13 gold badges123 silver badges164 bronze badges

Generally the usage of 'this' is reserved for instance variables and methods, not class methods ...

"class methods cannot use the this keyword as there is no instance for this to refer to..."

http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Here's a trivial example ...

public class Person {
    private String  name;
    private int     age;
    private double  weight;
    private String  height;
    private String  gender;
    private String  race;

    public void setName( String name ) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void setAge( int age) {
        this.age = age;
    }

    public int getAge(){
        return this.age;
    }

    public void setWeight( double weight) {
        this.weight = weight;
    }

    public double getWeight() {
        return this.weight;
    }

    public void setHeight( String height ) {
        this.height = height;
    }

    public String getHeight() {
        return this.height;
    }

    public void setGender( String gender) {
        this.gender = gender;
    }

    public String getGender() {
        return this.gender;
    }

    public void setRace( String race) {
        this.race = race;
    }

    public String getRace() {
        return this.race;
    }

    public void displayPerson() {
        System.out.println( "This persons name is :"    + this.getName() );
        System.out.println( "This persons age is :"     + this.getAge() );
        System.out.println( "This persons weight is :"  + this.getWeight() );             
        System.out.println( "This persons height is :"  + this.getHeight() );
        System.out.println( "This persons Gender is :"  + this.getGender() );
        System.out.println( "This persons race is :"    + this.getRace() );
    }
}    

And for an instance of a person ....

public class PersonTest {
    public static void main( String... args ) {
        Person me = new Person();
        me.setName( "My Name" );
        me.setAge( 42 );
        me.setWeight( 185.00 );
        me.setHeight( "6'0" );
        me.setGender( "Male" );
        me.setRace( "Caucasian" );
        me.displayPerson();
    }
}

answered Oct 24, 2012 at 18:49

Edward J BeckettEdward J Beckett

4,9711 gold badge39 silver badges40 bronze badges

In case of member variable and local variable name conflict, this key word can be used to refer member variable like,

public Loan(String type, double interest){
this.type = type;
this.interest = interest;
}

answered Apr 29, 2014 at 13:54

if you have knowladge about c,c++ or pointers, in that language this is a pointer that points object itself. In java everything is reference. So it is reference to itself in java. One of the needs of this keyword is that:

Think that this is your class

public class MyClass
{
      public int myVar;

      public int myMethod(int myVar)
      {
           this.myVar = myVar;        // fields is set by parameter

      }


}

If there is not this keyword you it is confused that this is paramter or class field.When you use this.myVar it refers field of this object.

answered Jan 3, 2012 at 7:45

oldTimesoldTimes

2593 gold badges4 silver badges13 bronze badges

I would like to modify your language. The this keyword is used when you need to use class global variable in the constructors.

public class demo{
    String name;
    public void setName(String name){
        this.name = name; //This should be first statement of method.
    }
}

this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

One more thing that should be in mind is that this keyword might be the first statement of your method.

What keyword in Java is the name of a reference that an object can use to refer to itself?

Adi Inbar

11.7k13 gold badges53 silver badges69 bronze badges

answered Jan 3, 2012 at 5:22

Abhendra SinghAbhendra Singh

1,9594 gold badges24 silver badges46 bronze badges

This is used in java. We can use in inheritance & also use in method overloading & method overriding. Because the actual parameter or instance variable name has same name then we can used this keyword complsary . But some times this is not same as when we can not use this keyword complsary..... Eg:- class super { int x; super(int x) { this.x=x } }

What keyword in Java is the name of a reference that an object can use to refer to itself?

G5W

35.1k10 gold badges41 silver badges74 bronze badges

answered Aug 20, 2013 at 3:23

What is reference to an object in Java?

A reference is an address that indicates where an object's variables and methods are stored. You aren't actually using objects when you assign an object to a variable or pass an object to a method as an argument. You aren't even using copies of the objects. Instead, you're using references to those objects.

Which of the following keyword is used to reference of the object?

The ref keyword indicates that a variable is a reference, or an alias for another object.

What is the keyword that is used by the method to refer to the object that invokes it?

2. Which keyword is used by method to refer to the object that invokedit? a) importb) catchc) abstractd) thisAnswer: dExplanation: this keyword can be used inside any method to refer to thecurrent object.

What is a keyword used to reference the current instance of an object?

The current instance of an object is the instance in which the code is currently executing. You use the Me keyword to refer to the current instance.