Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

my instance variable values can be diffferent from my buddy's values.

 
Tomendra Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

i am studying head first java and in chapter 2 there is a line "my instance variable values can be diffferent from my buddy's values."

i am not able to understand it,can anybody help me with some example.


thank you.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my car is (probably) a different color than your car, even if we both own ford mustangs.

an 'instance' refers to a specific object that has been created of a given class. each and every car that rolls of the assembly plant is an instance of the car, with it's own color, VIN, odometer reading, etc, but they are all of the same 'Ford Mustang' class.
[ October 27, 2008: Message edited by: fred rosenberger ]
 
Tomendra Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need some more clarification, still i have not understood.

i am repeating my query, "object says-- my instance variable values can be diffferent from my buddy's values."

what does that mean "buddy's values"
 
Kyle Teeger
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the book is referring to "buddy's" as another implementation of the respective class. For example, you have your blueprint/superclass named Car with an String instance variable of Color. Now you have two classes that extend Car named Mustang and Bronco. Both of the subclasses will have access to their own Color instance variable from Car, however; it can have different values.

Mustang.Color = "Red"
Bronco.Color = "Blue"

Hope this helps.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They simply mean that two different instances of the class can have different values.

Let's say there exists a factory method called "getMustang()". You call it like this:

Mustang myMustang = getMustang("Blue");

you get a blue mustang. Now you're a nice guy. you have a buddy or friend who wants a mustang, but he wants a red one. So, you can call the method again:

Mustang myBuddysMustang = getMustang("Red");

now you have two Mustang objects - one is yours, and one is your buddy's. Both have a 'color' instance variable to hold what color the Mustang is, but the two (yours and your buddy's) hold DIFFERENT values.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic