We are dealing with primitives here, so it means i2 is now 3 as well?
so i2 is now 4??? then what's the point of the line above?
What does + i1 mean? 1+i1??
Now we are dealing with Object.
is val a method here? since it's written that way.
v2 also points at v1, refer to the same object? or v1 points at v2?
Then I don't get why it's 6 instead of 5?
how come v1.val also become 6? So the line Value v2=v1, it's not v2 points at v1, it's v1 points at v2?
is val a method here? since it's written that way. val is an instance variable of the class. You can tell the difference because methods end with parentheses.
v2 also points at v1, refer to the same object? or v1 points at v2? v2 does not point at v1. v1 and v2 both reference the same object.
Classes can have both methods and variables. You have two classes defined: Value and Test. You've defined a method in Test (main). Test has no variables. Your Value class does not define any methods but it does define a variable of type int called val. If I understand your question correctly, the "actual Object" does not have a value of 5. The "actual Object" has a member variable of type int that has a value of 5 and the value changes to 6 when you change it:
I don't think I've yet encountered that an object reference followed by an instance variable. What's the purpose of it? Does it just mean that v1 is an object reference, and the actual Object has the content of 5. I assume int val=5?
On that line, you are:
And when we have: Value v1 = new Value(). Does it just mean we create an object reference, not the actual object then?
So v2 and v1 now reference to the same object that has the value 5?
Campbell Ritchie wrote:You have misunderstood our requirement. We need you to tell us where the code came from.
Cheryl Scodario wrote:Thanks, Tom. This is not my code, so I just cited the source. . . .
Classes can have both methods and variables. You have two classes defined: Value and Test. You've defined a method in Test (main). Test has no variables. Your Value class does not define any methods but it does define a variable of type int called val. If I understand your question correctly, the "actual Object" does not have a value of 5. The "actual Object" has a member variable of type int that has a value of 5.
Cheryl Scodario wrote:When you say "member variable" of an object, do you mean it is like a trait of the Value Object. Like it is part of it, but not THE object? But the object includes the value 5?
fred rosenberger wrote: Methods are what the object can do, and member variables hold the states. For example, if you wrote a Car class, you might have methods like startEngine(), speedUp(), setReverseGear(), etc. But you need member variables to hold things like color, or modelYear or licensePlateNumber.
Thank you. I missed that. Sorry for my mistake.
mike ryan wrote: . . . Cheryl has added the link to the source code in question on her original post
Stephan van Hulst wrote:Cheryl, the state of an object is all the information about that object that is visible to the outside world.
Cheryl Scodario wrote:What does it mean by "a Java method can update an object's state, but it cannot replace the contents of an object reference." So the contents of an object reference is just the Object itself? Also, I have always been sort of confused at the definition of state. So state is just like a characteristic/trait of an Object?
Rob Prime wrote:An example:
The reason clearWrongly doen't work is because the sb on lines 13 and 15 is not the same as the sb from lines 5, 6, and 7. They are referencing the same object (until line 15) but they are not 100% the same.
fred rosenberger wrote:
Say I have a piece of paper with a home address on it. the boss says to me "go to that address and tell me what name is on the mailbox". I do that, and come back with "rosenberger".
Now I give you a photocopy of that piece of paper. then the boss says to you "go to that address and tell me what name is on the mailbox". You would go to the same house, and also come back with "rosenberger".
Now the boss says "erase the address on your piece of paper, and write this NEW address down. then go to THAT address, and give me the name on the mailbox". You may come back with "Scodario". The boss says "OK, you're done, throw your paper away".
This is how Java works. If you pass an object reference into a method, the method works with a copy of the address. the method can't change the address the calling method has, no matter what.
Cheryl Scodario wrote:Thanks, Rob! Just one follow-up question: Is the reason why sb on lines 13,15 is different from 5,6,7 because the object sb from 5,6,7 starts with "Hello World" (a parameter variable), but from 13,15, it starts with nothing?
Cheryl Scodario wrote:what's your point when the boss says erase the address and write a new one?
Cheryl Scodario wrote:My second question is in my original question, "the contents of an object reference" is then the Object's address? so it's not the Object itself?
Cheryl Scodario wrote:I feel like we been talking about the state (what's visible to the outside world and what affects the behavior of the object), so what is the Object really? i know how we can create one and all that. But all this time, we just use object reference and change states. and of course we have methods to dictate the behaviors of the object, but do we really see the Object tangibly?
Rob Prime wrote:
No, it's different because it starts with "new". Well, actually, it's different because the reference is pointing (I hate that word in this context) to a different object. I could have said "sb = new StringBuilder("Hello World!")" and it still would be different, even though they contain the same characters.
Rob Prime wrote:Sure. The old object the reference was pointing to will have its reference count decreased by 1 and if that hits 0 it's eligible for garbage collection. Note that in my example there are two references to the StringBuilder object before line 15 starts - one in the main method and the method parameter.