GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Aug-2006, 01:41
_Y_ _Y_ is offline
New Member
 
Join Date: Aug 2006
Posts: 11
_Y_ is on a distinguished road

Passing an object as a reference. What is wrong?


All books state that an object is passed to a function as a reference. This is a simplest program:
JAVA Code:
package a;
import javax.swing.JList;
public class Atest {
   static Atest aaa;
   public Atest() {
      String[] s = {"Test_A"};
      JList jList = new JList(s);
      System.out.println(jList.getModel().getElementAt(0));
      subr(jList); // Change content of the list
      System.out.println(jList.getModel().getElementAt(0));
   }
   public static void main(String[] args) {		
      aaa = new Atest();
   }
   private void subr (JList jL) {
      String[] m = {"Test_1"};
      jL = new JList(m);
   }
}
The output is:
Test_A
Test_A


IMHO, passing by the reference, must produce
Test_A
Test_1


What do I miss? Thank you
  #2  
Old 29-Aug-2006, 11:58
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 926
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Passing an object as a reference. What is wrong?


Java manipulates objects 'by reference,' but it passes object references to methods 'by value.' -- David Flanagan, O'Reilly's "Java in a Nutshell"

see an example of the difference here:
http://www.javaworld.com/javaworld/j...0526-pass.html
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing an object as argument to a function? Ahas CPP / C++ Forum 2 25-Jul-2006 09:11
How do I reference an undeclared object ? Fermat CPP / C++ Forum 8 26-Apr-2006 22:54
G++ question... TreyAU21 CPP / C++ Forum 10 24-Feb-2006 08:45
How to interpret characters as they are being entered? nkhambal C Programming Language 18 14-Feb-2006 10:41
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:24.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.