Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 references in java?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Justin_




PostPosted: Sat Apr 29, 2006 9:21 pm   Post subject: references in java?

If I had a procedure that I want to pass a member variable to, but since its not always the same member variable I have to pass it which variable it is, the fact still remains that it is a formal variable and it won't affect the member variable. How is the best way to deal with this in java?
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Sat Apr 29, 2006 10:48 pm   Post subject: (No subject)

If there's a way around your design, such that you can avoid this, then by all means refactor that code, otherwise, you can make use of java reference objects. Specifically java.lang.ref.WeakReference. This technically does not save the "reference" but creates an object that acts like a reference to any "object" of a particular type.

But if you need to work with references only (i.e pointer-like stuff), then you might have to use reflection, and I strongly suggest you avoid this. Through reflection one can easily bypass the type safety of the java language.
Justin_




PostPosted: Sun Apr 30, 2006 4:27 pm   Post subject: (No subject)

Hmm, interesting. I suppose I change the groundwork, but I would think using pointers is the most efficient way. Why did they take out pointers/references in java?
rizzix




PostPosted: Sun Apr 30, 2006 5:44 pm   Post subject: (No subject)

references are pointers.. they just have a different set of rules.. the most significant of which is that they can only point to "objects".
wtd




PostPosted: Sun Apr 30, 2006 6:45 pm   Post subject: (No subject)

They didn't remove pointers from Java. All variables for object types are pointers. You just can't do arbitrary arithmetic on them. Additionally, since they are only pointers for object types, and no pointers for non-object types, there is no need for an additional syntax (*) as in C and C++.

The presence of pointers in Java is amply demonstrated by the presence of NullPointerException.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: