Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

this.variable vs this.getvariable What's the difference between this three lines of code?  RSS feed

 
Marco Paulo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.

I'm new at Java and I'm still trying to understand the difference between this.variable vs this.getvariable.
I've made a simple Java code, and for example, what's the difference between line 39 and 40 and 41? They all result in the same output.



So, when should I use this and not. I understand that this refers to the current object, so, ins't it better to always use this (like in line 39)?
Bests regards
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can usually omit the this. bit (unless you are in a constructor or similar like this)If you write variable, inside the same class, that gets direct access to the variable. If you use getVariable that gives you whatever your getXXX method returns. It might not necessarily be the same. Let's have a look at my Kettle classIf you use w in line 7, you get direct access to the Water object. But the getWater method does not return w. It returns another (identical) Water object, so code outside this class can play with the Water object without the Kettle object changing its state. So using getWater and w inside the Kettle class behave differently. Look at these two examples:-They are the same except for line 9. But they will behave differently if you try this:I shall let you work out what the two printouts will be depending which version of the heat() method you used.

And welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't change your package name every time you revise your class. Revision dates should go in the /**documentation comments*/
Don't call public methods from the constructor.
Don't use an int for a telephone number. Telephone numbers are not numbers in the sense that you can do arithmetic with them. Create a TelephoneNumber class.
Don't use if(b) return true; else return false;. Simply write return b;
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote: . . . Create a TelephoneNumber class. . . .
And it appears there already is a TelephoneNumber interface.
 
Marco Paulo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell.

Thank you very much for your answer

I'll process the info you gave me
 
Gautam Joshi
Greenhorn
Posts: 18
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here, the difference is this.creditro is referring the instance variable value and this.getCredito() is calling a method of this object..

just understand what is happening here..

at Line no. 39 : this.credito = this.getCredito() + valorrecebido;

this.credito is refering to this instance variable, which is being assigned at this line...with the value of cerdito + valorecebido,
here, we assume that credito = 0; and valorecebido = 50 so now at the line no. 39 credito value will become 50, which is 0(zero) before the execution of this line;

goining furthure,

at Line no. 40 : this.credito = getCredito() + valorrecebido;

Here, this.credito = 50 and will be incemented by 50.. and this .credito = 100, How??

Let's see, credito is being assigned with getCredito() + valorrecebido(), here, getCredito()
will give us a value = 50 ( because, the above line has just assined a vaue of 50 to this.credito which is first 0, check it.)
and valorrecebido value is in this scope is 50 what we assumed ,
so the ultimate value of this.credito will be 100 after the line no. 40 is being executed.

at Line no. 50 : the same thing will be repeted and the ultimate value of this.credito will become 100 + 50 = 150.

 
Marco Paulo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gautam Joshi wrote:

Here, the difference is this.creditro is referring the instance variable value and this.getCredito() is calling a method of this object..

just understand what is happening here..

at Line no. 39 : this.credito = this.getCredito() + valorrecebido;

this.credito is refering to this instance variable, which is being assigned at this line...with the value of cerdito + valorecebido,
here, we assume that credito = 0; and valorecebido = 50 so now at the line no. 39 credito value will become 50, which is 0(zero) before the execution of this line;

goining furthure,

at Line no. 40 : this.credito = getCredito() + valorrecebido;

Here, this.credito = 50 and will be incemented by 50.. and this .credito = 100, How??

Let's see, credito is being assigned with getCredito() + valorrecebido(), here, getCredito()
will give us a value = 50 ( because, the above line has just assined a vaue of 50 to this.credito which is first 0, check it.)
and valorrecebido value is in this scope is 50 what we assumed ,
so the ultimate value of this.credito will be 100 after the line no. 40 is being executed.

at Line no. 50 : the same thing will be repeted and the ultimate value of this.credito will become 100 + 50 = 150.



Hi Gautam, thank for your answer.

I only wanted to know the syntax difference, because line 39,40 and 41 are supposed to be just one of these three lines, but as the result was the same with any of those three, I want to know if there is a difference.

For example, is this.credito=this.getCredito()+valorrecebido equivalent to this.credito=getCredito()+valorrecebido; , which means, is this.getCredito() equivalent to getCredito()... In that example, could I use anyone of those 3 without problems, or is one form more or less correct to the other? (The purpose of that method is to add valorrecebido to credito).

Best regards.
 
Gautam Joshi
Greenhorn
Posts: 18
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marco,

yes, You are right..., here in this example there is no problem to use this. But suppose,
if there is a method overridden concept is being applied then it would be fine to use this and super keyword explicitly
with the both name and method to target which one we want to call. OR if you have local variable of the same
name in same scope then to address the instance variable you should use this keyword otherwise local variable value will be used..
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco Paulo wrote:So, when should I use this and not. I understand that this refers to the current object, so, ins't it better to always use this (like in line 39)?

Personally: I always use this for instance members - unless I forget to ; but if I do, it's usually in basic enough situations that there's no question what's intended. I'm pretty religious about it with method calls though.

There's only one exception I have to that rule (and I have to admit, my approach is inconsistent): If I call a static method, I will prefix it with the class name (even from within the same class); if I reference a static constant (from its class) I simply refer to it by name. But that's because I use the "caps only" convention for naming my constants.

Winston
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Personally: I always use this for instance members

This for me as well (pun intended). That way, the references are consistent everywhere, and not dependent upon whether the this. has to be there or not. I like consistency.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!