It may be done but doesn't appear so... In some other languages, eg C or VB, you can pas parameter either by value or by reference.
If you pass by value, the program makes a local copy of the variable and any changes made in the method apply only to this local copy. If you pass by reference, changes made within the method apply directly to the variable that is passed so you can see the object changed when you come back to calling method. I believe Java doesn't make a local copy of the method parameter, so uses it "by reference", but discards any changes made within this method. If you run Shankar's code, you can see that the changes made in fill() methods do not apply to the objects in main() - it is exactly what happens in other programs when you pass variable by value. If you wrote similar programs in VB or C with functions that take parameter ByRef or pointer, the output in main would show that the object have been changed by fill(). So I think the pass by reference works only one-way but I may be wrong... Witold On 31 May 2002 at 10:42, Ezekiel Ebiurhie wrote: > Shankar, > > When you define an instance of variable and later passed this variable > to a method u have successful done what is called a referenced > parameter passing. Which is what u have successful done. > > Ezekiel > > -----Original Message----- > From: H Shankaranarayanan [mailto:[EMAIL PROTECTED]] > Sent: Friday, May 31, 2002 9:31 AM > To: JDJList > Subject: [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 ================================== Witold Iwaniec Sr Software Developer NovaLIS Technologies [EMAIL PROTECTED] http://www.novalistech.com To change your membership options, refer to: http://www.sys-con.com/java/list.cfm
