![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,033
Rep Power: 5
![]() |
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;
.
.
}
__________________
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 |
|
|
|
|
|
#12 |
|
Expert Programmer
|
Thanks for all the suggestions, but I think I've found a cleaner solution. I'm planning to use custom events.
|
|
|
|
|
|
#13 | |
|
Programmer
Join Date: Nov 2004
Posts: 84
Rep Power: 4
![]() |
Quote:
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 |
|
|
|
|
|
|
#14 |
|
Expert Programmer
|
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). |
|
|
|
|
|
#15 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#16 |
|
Expert Programmer
|
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.
|
|
|
|
|
|
#17 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,033
Rep Power: 5
![]() |
Quote:
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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|