You are correct Shankar.  The problem here is that the overloaded fill()
method does not alter the state of the class.  String fillMe and Integer
fillMe are both local to the methods, they are not instance variables.
Alter your Class such that those methods will set and instance variable.
e.g

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


    /**
     *  Description of the Method
     *
     *@param  fillMe  Description of the Parameter
     */
    public void fill(Integer fillMe)
    {
        ifillMe = 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();
        }
    }
        private String sfillme = null;
        private Integer ifillme = null;
}

-----Original Message-----
From: H Shankaranarayanan [mailto:[EMAIL PROTECTED]]
Sent: Friday, May 31, 2002 12:11 PM
To: JDJList
Subject: [jdjlist] RE: AW: Java : pass by reference???


But a variable is all u have to handle a object rite?
so for me a variable is an object of course unless its a primitive.
I am not convinced here. If you  could explain it in a better way
it would be great.


-----Original Message-----
From: Lesden, Kim [mailto:[EMAIL PROTECTED]]
Sent: Friday, May 31, 2002 3:33 PM
To: JDJList
Subject: [jdjlist] AW: Java : pass by reference???


Shankar, you always pass Objects by reference, only primitives (int, char,
...) are passed by value since they are not real objects in Java. In your
sample, you mix up variables and objects. Variables are just names for
objects.

When you call a method with parameters you pass the object behind the
variable not the variable itself. The parameter variable within the
fill-method (fillMe) is not the same as the local variable in the main
method (intFill). When the fill-Method is called the value of the
intFill-Variable, which is a reference to an Integer-Object, is copied to
fillMe-Variable. Changing the value of the intFill-Variable to a reference
to a new Integer-Object does not change the value of the intFill-Variable.
It is still referencing/pointing to the original Integer-Object.

Regards,
Kim

-----Ursprüngliche Nachricht-----
Von: H Shankaranarayanan [mailto:[EMAIL PROTECTED]]
Gesendet: Freitag, 31. Mai 2002 09:31
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


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