• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

this keyword

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic