Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 2nd, 2006, 11:42 PM   #11
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,033
Rep Power: 5 lectricpharaoh will become famous soon enough
Use pointers- oh, right, you're using Java; you can't.

Perhaps you could wrap the reference in a larger object. Then you can pass a reference to this object between your classes. If you want to reassign a different type, you can do it, because all instances of the wrapper object will share the same reference member (since they are the same object). As long as you don't reassign to the wrapper reference itself, this should work.
class Wrapper
{
  abstractType foo;
  .
  .
}
I know it's a clumsy way to do what you want, but it gets the job done, and you don't have a whole lot of options, given Java's frustrating lack of pointers. Another option is to use an array, and pass the array reference around. If you've got multiple objects you want to pass around, or the use of a wrapper class is, for some reason, not appropriate, this might be a more viable option.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is online now   Reply With Quote
Old May 3rd, 2006, 12:17 AM   #12
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Thanks for all the suggestions, but I think I've found a cleaner solution. I'm planning to use custom events.
titaniumdecoy is offline   Reply With Quote
Old May 3rd, 2006, 6:51 PM   #13
groovicus
Programmer
 
Join Date: Nov 2004
Posts: 84
Rep Power: 4 groovicus is on a distinguished road
Quote:
Use pointers- oh, right, you're using Java; you can't.
What do you mean? Java uses pointers, they're just disguised (they are called references). If Java didn't use pointers, you would never get a "null pointer error"....

Collection c; defines a pointer to a collection, and would be the equivalent of Collection *c. When passing objects, the system is actually only passing a pointer to the object. Linked lists use pointers, maps use pointers, DAGs use pointers, etc.

I apologize for being pedantic. "Java has no pointers" is one of those old wive's tales that never seems to die.
__________________
HijackThis Team-SFDC
groovicus is offline   Reply With Quote
Old May 3rd, 2006, 7:43 PM   #14
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
What lectricpharaoh means is that Java "pointers" don't work the way true pointers do (like those in C/C++). As I understand it, a C pointer stores a reference to a location in memory, while a Java "pointer" is a reference to a particular object. If the location in memory that is referenced by a pointer is updated to reflect the location in memory of a different object, any similar pointers will now point to a different object. In Java, when a new object is created, each "pointer" must be updated explicitly to point to this new object or they will continue to point to the old object. lectricpharaoh is correct that using C-style pointers would make what I'm trying to achieve much easier.

And as for my original problem, I am now using custom events along with a wrapper class (as lectricpharaoh suggested).
titaniumdecoy is offline   Reply With Quote
Old May 3rd, 2006, 8:33 PM   #15
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
lectricpharaoh's solution is the java equivalent of a pointer to a pointer.

I stand by that it would make sense to just have a method to update the Data object referenced by the various components.

Assuming you have one class which holds references to your GraphPanel and ControlPanel:
private void updateData(Data newData) {
     mySuperThing.setData(newData);
     theUltraObject.setData(newData);
}

In fact, you might want to consider a base class for behavior relating to a consumer of Data objects. Notably, the setData method.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old May 3rd, 2006, 8:38 PM   #16
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
That's just what I mean when I say I'm using custom events: I implemented a Listener interface in any classes that need to access the data forcing them to implement an updateData() method. These classes are stored in a List in the Wrapper class along with the data. When I need to notify the Listener classes that the data has changed, I call the fireEvent() method in the Wrapper class that iterates through all the Listener classes and calls updateData() on those classes. It works well, although it's a little complex.
titaniumdecoy is offline   Reply With Quote
Old May 3rd, 2006, 11:47 PM   #17
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,033
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by groovicus
What do you mean? Java uses pointers, they're just disguised (they are called references). If Java didn't use pointers, you would never get a "null pointer error"....

Collection c; defines a pointer to a collection, and would be the equivalent of Collection *c. When passing objects, the system is actually only passing a pointer to the object. Linked lists use pointers, maps use pointers, DAGs use pointers, etc.

I apologize for being pedantic. "Java has no pointers" is one of those old wive's tales that never seems to die.
While it certainly uses pointers in the bytecode it generates, there is no source-level equivalent. References are similar, yes, but haven't you noticed that you can only create such references to classes (or arrays, which are essentially classes under Java)? If you can show me how to create a reference to a fundamental Java type such as int or double, that might convince me that Java offers true pointers.

Also, bear in mind that Sun bent some of the rules when they wrote the Java libraries. Look at the String class; it supports the foo + bar syntax for string concatenation, even though Java doesn't actually provide for operator overloading. My point is that just because a feature may be used in the Java libs doesn't mean that code written by regular programmers can use those features. Try writing a class that does not inherit from any Java class (besides Object), nor implement any Java interfaces, and show me how you can support operator overloading in that class. Thus, even if 'pointers' are used in some of the Java libs (and I suspect they are references instead; see my above paragraph about references to fundamental types), it doesn't mean we can directly use them, except through such library code.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is online now   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:19 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC