• Post Reply Bookmark Topic Watch Topic
  • New Topic

this keyword  RSS feed

 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the below discussion question posted for my class. I am pretty sure I understand the use of the this keyword. I stated that when using this, it is referencing that particular method. What I don't understand is the 5 instantiated objects and 5 different variables. Can someone help explain? Or provide an example of what the below statement is showing. I can't picture what is being asked here.

An instance method might contain a statement like this.weight = 1.0; but if that method's class currently has five instantiated objects, there are five different variables called weight. How can we determine which one is getting the new value?


Thanks
Amie
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The keyword this refers to the current object, not a method. An instantiated object just means you have used to the new keyword to create an object. Does this help clarify the question?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each of the five objects has a weight. They do not necessarily weigh all the same. You use this.weight to distinguish the weight of the current object from any other weight.

If you have not declared a local variable called weight or a parameter called weight, then the only possible weight you can find turns out to be this.weight. In which case writing this. is redundant. But some people like to write this. regardless so as to make absolutely sure they are referring to the weight of the current object (i.e. the instance field of that name). If you write this.fieldName you always get the field as opposed to local variable/parameter.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if I understand you correctly there are 5 different object all with a weight variable?

Would it be like this:


etc?

Or how do you make 5 instances of an object?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.Obviously you won't want those 5 variables lying loose like that; you will want to put them into a data structure.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amie Mac wrote:So if I understand you correctly there are 5 different object all with a weight variable? . . .
Yes.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't make instances of an object. You make instances of a class.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:No.
Obviously you won't want those 5 variables lying loose like that; you will want to put them into a data structure.


So these are instances of the class WeightObject correct?
If you had a weight variable declared like:

double weight;

could the instance then look like this:



?

I am trying to understand it as the discussion question is written.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amie Mac wrote: . . .
could the instance then look like this:
. . ..
No.

You never see what an instance “looks like”; you only ever see the class it was created from.
 
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!