• Post Reply Bookmark Topic Watch Topic
  • New Topic

I have a question on this in methods  RSS feed

 
Bill foster
Ranch Hand
Posts: 66
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the book I'm reading it says that I do not need the this. in the methods but it is a good practice to do this(this as in this not in code).
Also in the book it says that this is a implied parameter and that every method has one implied parameter. So when I use my instance field why does the compiler automatically attaches this to the field.
Why is a implied parameter in front of a instance field. This is a bit confusing to me because aren't parameters in the methods like in the withdraw method :public void withdraw(double amount)

Can someone please clarify for me? See I told you I'll be working on a bank account program!!! I only wish it could make real money!!!


thanks,

Bill

 
Vaibhav Sagar
Ranch Hand
Posts: 35
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill,

Even though 'this' is an implied parameter, it still points to an object(the object that called the method). So for accessing the instance fields of that particular object you will need to 'objectReference.instanceField'. It doesn't matter whether the object reference was implicit (this.balance) or explicit(BankAccount ba = new BankAccount(); ba.balance = 100;).
Example-
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaibhav Sagar wrote:(the object that called the method).

Not quite the right way to say it. Rather, this refers to the current object on which the method was invoked. It's entirely possible that a different object could have called/invoked the method.

Another way to look at a method is as a "message." That is, when you invoke/call a method on an object, you are sending a message to that object asking it to do something. In other words, this refers to the object that receives the message and does something. The idea that a method is a message is a pretty old one that isn't used that much any more.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reference to the current object, this, becomes significant when you have a method parameter that has exactly the same name as a field of the object. For example,

The use of this. is required here so that Java can distinguish between the field and the parameter that has the same name as the field. Without the this. to differentiate the two, the statement will take balance to mean the parameter on both sides of the assignment statement, which isn't what is intended.

If a parameter has the same name as a field, the parameter is said to hide shadow the field from any code inside the method. The this. reference unhides unshadows/unshades(?) the field so that you can use it instead of the parameter with the same name.

Edit: Campbell has kindly reminded me that the correct term is "shadow" in this case. Thanks, Campbell.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill foster wrote:it is a good practice to do this...

Yes, it's a good practice that increases clarity of intent when you have a few lines of code in the method and some local variables as well. Using this makes the field stand out from the locals. The practice can also help prevent bugs later on if the code is modified and a parameter or local variable with the same name is introduced.

Bill foster wrote:Also in the book it says that this is a implied parameter and that every method has one implied parameter. So when I use my instance field why does the compiler automatically attaches this to the field. Why is a implied parameter in front of a instance field]

It's not so much like that. Rather, the compiler will try to resolve names by looking to see if a name is a local variable or parameter first. If the name can't be found in the local method scope, the compiler will then use the implicit this reference to try to resolve the name at the instance scope.

This explains the shadowing effect because the compiler will "see" the name that's in the local scope first and assume that's what you were referring to if you don't explicitly use this to unshadow a field with the same name.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill foster wrote:. . . it is a good practice to do this . . .
After you have gone to lots of trouble to choose a really good name for your field, you now have an opportunity to expose that name as the parameter in the constructor or a setXXX method. Using this.xxx allows readers of your javadoc documentation to see the good names and it makes the intent of the method clearer. If you write this sort of thing, the badly‑named parameter will be all a user sees:-Another little stylistic advantage.
Whether a setBalance method is good design is a different matter.
 
Bill foster
Ranch Hand
Posts: 66
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help guys, I think when I took java a long time ago I had some issues with this. But with the help of all of you I think I got it.
This is used to sent a message for the present object to do something, methods are actions that object can do.

And you can use this to tell the compiler that if the method parameter has the same name as the instance field the you can tell that the one with the this. is the instance field.

If the parameter has the same name as the instance file the complier will shadow the instance field that means it will assume that the programmer is talking about the parameter. To prevent this we use the this to tell the complier that we are talking about the instance field.

My questions:

So if we did not use "this" and we ran the method would we get a error code on the complier?

And if the programmer did not know about shadowing the programmer would assume the state of the object was changed even if it did not without setting a getter method and actually printing it that is if the complier did not error.

Do I have it all correct?
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not going to be compiler error. It will be a bug, otherwise known as a logic error, though because the program won't work as intended. You'll be assigning the value of the parameter to itself, which in effect does nothing to it.
 
Vaibhav Sagar
Ranch Hand
Posts: 35
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather, this refers to the current object on which the method was invoked. It's entirely possible that a different object could have called/invoked the method.


Could you please give an example for this scenario?

Thanks.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're looking for an example where code in class A calls a method of an object of class B? I don't think it should be too hard to make up such an example -- why don't you give it a try?
 
Bill foster
Ranch Hand
Posts: 66
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my bank account program I have a tester class named BankAccountTester(A class) to test my methods and constructors from the BankAccount class(B class) . when I create an object from the BankAccount class(B class) and call the method and create object in the BankTester class(A class) it uses the methods from the BankAccount Class(B Class). Here is a example:

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't write so many empty lines; they make the code harder to read, not easier.
I would add a toString method to your account class. Then you can simply write System.out.println(myAccount);
I suggest you add this sort of line to your testing code:-
myAccount.deposit(-1_000_000);
The _ underscores are only permissible in Java7+.
 
Bill foster
Ranch Hand
Posts: 66
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Richie I'll work on not creating too many empty lines.

So when you say is I need a toString (), is this a good example:



So why would you need to use the underscore symbol to test the class?

Thanks for the input Richie!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need underscores; they simply make the numbers easeir to read.
No, that isn't a good example. For one thing you should override the Object#toString() method. For another you have got your indentation all wrong and still too many blank lines. Also only write \n if somebody tells you they want the LF character. I would use %n which will get you the correct line end for your system. But you will have to use a different way to compose your String.Actually I would miss out the line breaks and return a single line.
 
Bill foster
Ranch Hand
Posts: 66
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the example from another website, I did not know what you mean and yes I was require to use the /n that was one of the requirements .
I'll fiddle around with the toString(), it is actually pretty nifty method to use.

Yeah I like lots of room, I come from a math background (master degree in mathematics) whenever I teach calculus I write big and messy like a math professor (I hate not having room). I'll work on not having too much dead space in my code.

Thanks Ritchie!!!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!