• Post Reply Bookmark Topic Watch Topic
  • New Topic

this keyword  RSS feed

 
tulshiram kamble
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is we can say "this" is used for memory reusability
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tulshiram kamble wrote:is we can say "this" is used for memory reusability


No. I don't even know what you mean by "memory reusability".

When describing the execution of Java code, there is a notion of a "current object." The purpose of the this keyword is to provide a way to refer to that "current object" in the code.
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No there is no memory reuse as the variable is not free nor any allocation takes place.
The "this" keyword just works like a reference to the current object as you do not have an explicit reference.

Hope this helps
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Javaranch
Next time onwards please UseAMeaningfulSubjectLine so your post receives maximum response.
 
Ronen boss
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.

For example, the Point class was written like this

public class Point {
public int x = 0;
public int y = 0;

//constructor
public Point(int a, int b) {
x = a;
y = b;
}
}

but it could have been written like this:

public class Point {
public int x = 0;
public int y = 0;

//constructor
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}

Each argument to the constructor shadows one of the object's fields — inside the constructor x is a local copy of the constructor's first argument. To refer to the Point field x, the constructor must use this.x.
 
tulshiram kamble
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as we know "this" hold address of current object suppose if we assign value to current object and we call methods with parameter as a same variable name
it will be changes to change d current object value
 
tulshiram kamble
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
memory re-usability means as we know current object have memory address ,when we call methods along with parameter have same variable name,
"this" will point or refer to current object so this current object address will be reusable again. i think your getting my point this statement.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tulshiram kamble wrote:as we know "this" hold address of current object


No, it is a reference to the current object. The JLS doesn't say anything about addresses.

suppose if we assign value to current object


Java does not provide any means of doing that.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tulshiram kamble wrote:memory re-usability means as we know current object have memory address


An object's memory address is never relevant or available in our Java code.

,when we call methods along with parameter have same variable name,
"this" will point or refer to current object


Yes.

so this current object address will be reusable again.


No idea what you mean here.

i think your getting my point this statement.


Nope.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!