File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes reference to same object.. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "reference to same object.." Watch "reference to same object.." New topic
Author

reference to same object..

Kevin Liu
Greenhorn

Joined: Aug 23, 2001
Posts: 2
i know this doesn't makes much sense, but i try to relate the prob using an example...i try my best to explain...
Lets say i have 2 classes : A Dog class and a Cat class
A Dog class which consists of a Leg object
(There is a Leg class which have 'number of legs' as an attribute as well as other instance variables..the Leg class also have several methods such as setNumLegs() to set the legs)
The Cat class consists of a dog as an object
In the Dog class, i can change the num of legs by using the method anyLegs.setNumLegs()..
The problem lies here..when i change the Leg object in Dog class how can i make it such that the Leg objects from the Cat class also changes?
in another word, whenever some values are changed, they are both updated and same in both Dog and Cat class...
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 965

Well one way of solving the problem is to have a "pet" base class from which both Cat and Dog are derived. You could have a variable in the Pet class for the number of legs, which both Cat & Dog would have access to. (If I understand the question correctly.)


Please ignore post, I have no idea what I am talking about.
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 104
Kevin,
I guess that i understood what you want.Now consider the implementation of your Leg class

This ensures that whenever you change the value of numOfLegs, it gets reflected in both class.
Let me know if it was according to your specification.
THANKS.
[This message has been edited by Bindesh Vijayan (edited September 01, 2001).]
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
I think instead what you would want to do is make the integer numberOfLegs in class Legs a static variable, like:
That way both Dog and Cat can have Leg objects, but all of those Leg objects are using the same numberOfLegs.
However, I think Christopher's suggestion is a better way to go, and more OO in design. However, if the requirement is that one class, in this example Cat, be composed of both Dog and Leg objects, then try making the numberOfLegs static.
Jason

[This message has been edited by jason adam (edited September 01, 2001).]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: reference to same object..
 
Similar Threads
Inheritance
TreeSet Question
How many objects Serialized?
Dan's Mock Comp. Exam 15, Question 24
Doubt Reg. Casting Object References