Shankar,
see the statement : fillMe = "test" in the method fill(String fillMe).
fillMe is local to the fill()-method but it has the reference to the object
'testFill'. fillMe = "test" does not modify the object referenced by the
fillMe, but it only change the reference of the variable fillMe to another
object "test" and
it looses the reference to testFill in the main()-method.
Because String and Integer do not provide content modification method, you
can not acceive what you want to do.
But try this :
    public void fill(StringBuffer fillMe)
    {
        fillMe.apend("test");
    }
you will see the difference.

Hope this help you to understand 'pass by reference' in java.

regards
Chao Ying

-----Ursprüngliche Nachricht-----
Von: H Shankaranarayanan [mailto:[EMAIL PROTECTED]]
Gesendet: Friday, May 31, 2002 9:31 AM
An: JDJList
Betreff: ÄjdjlistÜ Java : pass by reference???


class test
{
    /**
     *  Description of the Method
     *
     *@param  fillMe  Description of the Parameter
     */
    public void fill(String fillMe)
    {
        fillMe = "test";
    }


    /**
     *  Description of the Method
     *
     *@param  fillMe  Description of the Parameter
     */
    public void fill(Integer fillMe)
    {
        fillMe = new Integer(100);
    }


    /**
     *  Description of the Method
     *
     *@param  args  Description of the Parameter
     */
    public static void main(String args[])
    {

        try
        {
            test objTest = new test();
            String testfill = new String("main");
            objTest.fill(testfill);
            System.out.println("Fill me result:" + testfill);

            Integer intFill = new Integer(200);
            objTest.fill(intFill);
            System.out.println("Fill me result:" + intFill);

        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}


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.

--Shankar


To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm

To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm

Reply via email to