This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
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

Quick question about these 3 lines of code

 
andrew dale
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First line: setting the 0 object to size 30

Second line: Declaring variable X as pet 0 get size? <--- Confused part

Third Line: setting first pet to size of 8

 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have presumably already initialised the two objects in that array, otherwise you will suffer an Exception.
Whichever class is in that array has a size field, with get and set methods. You start by using the set method to give the size field of the 1st element the value 30.
Then you retrieve the same value from the get method (presumably still 30) and assign it to x. Because it is a primitive (int), there are now two copies of 30 around.
Then you get the 2nd element in the array and use its set method to assign 8 to its size field.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is also entirely possible that the setSize method has logic in it that prevents you setting the size to 30. Maybe it only allows sizes to between 1 and 10, and if you pass it something larger, it reduces it down to 10.

Without seeing the code of setSize(), we really don't know what it could be doing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic