This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can anybody explain this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can anybody explain this?" Watch "Can anybody explain this?" New topic
Author

Can anybody explain this?

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I have this code...

class TryEx
{
int i =10;
public static void main(String args[])
{
TryExex = new TryEx();
ex.i =10;
ex.method(ex);
System.out.println(ex.i);

}
void method(TryEx exref)
{
exref.i = 20;
TryEx h = new TryEx();
h.i = 100;
exref = h;
//exref = null; (1)
}
}
The output I expected was 100. But the output is 20. How?
For that matter even when exref is assigned null as in (1), the output is 20.
If you make the instance variable i, static then it is printing 100. This is understadable, considering that the class shares the class variable with all instances and hence any changes made by any object of the class will effect the class variable.
So can anybody explain this. Thanks
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Jairat:
I have this code...

class TryEx
{
int i =10;
public static void main(String args[])
{
TryExex = new TryEx();
ex.i =10;
ex.method(ex);
System.out.println(ex.i);

}
void method(TryEx exref)
{
exref.i = 20;
TryEx h = new TryEx();
h.i = 100;
exref = h;
//exref = null; (1)
}
}
The output I expected was 100. But the output is 20. How?
For that matter even when exref is assigned null as in (1), the output is 20.
If you make the instance variable i, static then it is printing 100. This is understadable, considering that the class shares the class variable with all instances and hence any changes made by any object of the class will effect the class variable.
So can anybody explain this. Thanks


exref is an object reference. so it has the memory address where the object ex is stored.
when exref.i=20, 20 is assigned to the variable i of object ex.
When exref=h, you make the variable exref point to a different object stored in a different memory location.
Here, the just the value the reference variable exref is changed not the contents of the memory location whose address is in exref.
Hope I did not confuse you.
Guys am I right ?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Originally posted by vasansrini:
....
Here, the just the value the reference variable exref is changed [b]not the contents of the memory location whose address is in exref.

[/B]

Yes, since the reference variable is now refering to a new location, shouldn't it also be refering the new values of the new location?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
But look at the scope of the variable exref.
Since it is a parameter to the method, the changes do not reflect in the calling method. It works the same way as if,
---------------------------
int i=10;
method(i);
System.out.println(i); // still prints 10
---------------------------
void method(int i) {
i=11;
}
---------------------------
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Observe that in method() the copy of the reference to class TryEx is being passed and untill or unless you make any changes(by operating) through this reference passed, the original value of the object is untouched.By just assigning the reference to some other reference will not have any effect on the original object.
Now as far as the static variables are concerned, they can be changed with any reference to that class.Also observe that class variable won't behave as static variables, as static variables are loaded once when the class is first loaded.Correct me if I am wrong.
void method(TryEx exref)
{
exref.i = 20;// Operating on the object through reference,hence value gets changed.
TryEx h = new TryEx();
h.i = 100;// This wd have effect,if i were static.
exref = h;
//exref = null; (1)
}

aparna kodali
Greenhorn

Joined: Oct 17, 2005
Posts: 6
Hi Jairat!
You are passing a copy of the reference variable 'ex' to the method.And you are assigning a new reference value 'h' to 'ex'.
When an argument is passed into a method,changes to the argument value by the method do not affect the original data.
But when the called method operates on the object via the reference value that is passed to it, the changes will be visible to the caller.
Hope this clarifies your doubt.
kodali.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Thanks, I understood, but I have one more doubt.
How come this concept doesn't apply to a String object. ie..
class TryEx
{
public static void main(String args[])
{
TryEx ex = new TryEx();
String str = new String("Hello"); (1)
ex.method(str);
System.out.println(str);

}
void method(String s)
{
s = "Bye";(2)
}
}

I created a string object at (1), and at (2), I am changing it.
But the output is still 'Hello'. ? Or is it because the String object is a local object?
Thanks for your patience :-)
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Strings are immutable in java. When the value of a String is changed, always a new object is created.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
But it will work for StringBuffer.

public class Test {
public static void main(String[] args) {
StringBuffer sb=new StringBuffer("Old Value");
method(sb);
System.out.println(sb);
}
static void method(StringBuffer sb) {
sb=sb.append("New Value");
}
}

the above code will print OldValueNewValue
Ken Lai
Ranch Hand

Joined: Aug 06, 2000
Posts: 30
Let me try to explain it to you by looking at the method you created:
<code>
void method(String s)
{
s = "Bye"; (2)
}
</code>
Whenever you have a method that passes parameters into it, try to remember there is always a second copy of the parameter created within your method.
In this case, let's say you call your method, passing in a reference of a String object to the method:
<code>
String strRef = "Hello" ;
method(strRef) ;
</code>
Here you have 2 copies of String reference pointing to the same object:
<code>
strRef
\ "Hello"
s /
</code>
Note:
strRef ===> this is the reference OUTSIDE of the method
s ===> this is the reference IN the method
Keep in mind that both are pointing to the same object. Now in your method you are setting s to point to a new String object (the "Bye" object), you are NOT changing the "Hello" object but instead pointing s to a new object:
<code>
strRef -> "Hello"
//this is the object created
s -> "Bye"
//another object created in
//within method()
</code>
Now s is pointing to the new object but strRef is not.
You might wonder why your first example managed to set the variable within the object. That's because both copies of the references point to the same object and when you try to modify an object through a reference. It doesn't matter which reference you use because any reference that points to the target object can be used to modify the object. The important thing to remember is there is always a second copy of reference created.
Hope this helps.
[This message has been edited by Ken Lai (edited August 06, 2000).]
 
GeeCON Prague 2014
 
subject: Can anybody explain this?