Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!

Marco Paulo

Greenhorn
+ Follow
since Jul 14, 2014
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
4
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Marco Paulo

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.
6 years ago
Hi Campbell.

Thank you very much for your answer

I'll process the info you gave me
6 years ago
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
6 years ago