In Java, pass by reference works about the same as in any other
language. When an object is passed to a method, the reference (address)
is given to the method. The method may directly manipulate the
reference if it desires, but it does not directly manipulate the object
itself.
To use your example:
H Shankaranarayanan wrote:
>class test
>{
> public void fill(String fillMe)
>
At this point, the reference contained in the main() variable testfill
is copied onto the stack (and pointed to by the local symbol fillMe).
So both the main() variable testfill and the fill() variable fillMe
contain the same value, the reference to a String object that contains
the string "main". This object may be used within the method. For
example, the method could call any public method of the object and it
would work as you expect. fillMe.equals("main") would return true.
>
> {
> fillMe = "test";
>
At this point, a new String object is created in the heap, and the
reference/address of the new object is stored in fillMe. So now, fillMe
points to a different object than testfill back in main(). Now,
fillMe.equals("main") would return false
>
> }
>
At this point, the fillMe variable disappears. So the "test" String no
longer has anything pointing to it and the space taken up by the object
becomes eligible for garbage collection. This same sequence occurs in
the fill() method with the Integer object.
> public void fill(Integer fillMe)
> {
> fillMe = new Integer(100);
> }
>
>
> public static void main(String args[])
> {
> test objTest = new test();
> String testfill = new String("main");
>
A String object is created on the heap and the reference is stored in
testfill.
>
> objTest.fill(testfill);
>
The contents of testfill (i.e. the reference to the String object in the
heap) is passed into the fill() method. The fill() method creates a
different object and manipulates its copy of the reference to point to
that new object. However, the reference stored in testfill is
unchanged. Therefore, testfill points to the same String as it did
before the call to fill().
>
> System.out.println("Fill me result:" + testfill);
>
The result is, of course, "Fill me result:main" The object that
contains the string "main" and the local symbol that contained a
reference to that object was never altered (although a copy of the
reference to the object was overwritten to refer to a different object).
>
>
> Integer intFill = new Integer(200);
> objTest.fill(intFill);
> System.out.println("Fill me result:" + intFill);
>
> }
>}
>
>
>That is a sample program i wrote to test this fact. The result is dependent
>on scope of the variable.
>So wots this pass by reference concept that every text book around the world
>states about Java.
>
>How does the pass by reference concept work anyways?
>
>I might have missed something here. If i did i would appreciate if anyone
>told me wot is it that i did miss.
>
>I was expecting this program to work otherwise but it does not.
>
I hope my explanation was understandable. Just in case, here is a
drawing to help
+============+ refers to:
| testfill | ----------------> "main"
+============+ ^
| |
| |
| reference passed into fill() |
| |
v |
+===========+ refers to: | At first, the reference in fillMe
| fillMe | ---------------------+ points (refers) to "main" but is
+===========+ | changed to point to "test"
|
V
"test"
As you can see, the reference in testfill is passed to fillMe. There is
no mechanism to pass the new reference back to testfill, so testfill
continues to refer to the same object before and after the call to fill().
Actually, as String is an immutable object, there is no way to change
the string that is referred to by testfill from within a method. You
could write this:
testfill = testfill.upcase()
This points testfill to a different object. But it is important to see
that the change to testfill does not take place within the upcase()
method. A reference value is returned from the method and that value
written back into testfill.
If you really want to change the value of a String from within a method,
here are two ways. The first is relatively easy:
class test
{
public void fill(String[] fillMe){
fillMe[0] = "test";
}
public static void main(String args[]){
test objTest = new test();
String[] testfill = {"main"};
objTest.fill(testfill);
System.out.println("Fill me result: " + testfill[0]);
// This will print: "Fill me result: test"
}
}
If you don't see how this works, draw a picture with boxes and arrows. You also have
to realize that, in Java, an array is an object of its own. So, using arrays, we are
increasing our level of indirection by one.
Another method is to create a mutable string object:
public class MutableString {
private String str;
public MutableString(String initialValue) {
str = initialValue;
}
public void setString(String newValue) {
str = newValue;
}
public String toString() {
return str;
}
// You can create other methods: length(), upcase(), equals(), etc.
}
Then, from within a method, you could change the value:
public void fill(MutableString fillMe) {
fillMe.setString("test");
}
This will now work the way you want it to.
Hope this helps,
Tomm
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm