What keyword in Java is the name of a reference that an object can use to refer to itself?
7 Show New! Save questions or answers and organize your favorite content. I was studying method overriding in Java when ai came across the
asked Jan 3, 2012 at 5:15
2
answered Jan 3, 2012 at 5:17
Anthony PegramAnthony Pegram 121k26 gold badges220 silver badges245 bronze badges 2
answered Jan 3, 2012 at 5:24
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 So, for example, if there is an object called Now,
it just so happens that answered Jan 3, 2012 at 5:24
this keyword have following uses
here parameter and instance variable are same that is why we are using this
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.
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 ...
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html Here's a trivial example ...
And for an instance of a person ....
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,
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
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.
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.
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 } }
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.
|