This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Referencing objects created by other classes. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Referencing objects created by other classes." Watch "Referencing objects created by other classes." New topic
Author

Referencing objects created by other classes.

Chase Manz
Greenhorn

Joined: Dec 08, 2005
Posts: 2
I will be the first to admit that I am really bad at java and I struggle heavily with it. Our teacher gave us this diagram of how to make our simulation(first my teacher is pretty much useless and impossible to get ahold of).
It has a lot of different classes and all of them reference each other.
For example my Main class creates a CallSystem object, that object creates 3 more objects Arrivals, Conultations, Scheduler. In my Scheduler class there is a variable that keeps track of the time. I need access to the time in the Scheduler object in my Arrivals object. I honestly have no idea how to do this. I tried to pass a reference to the Scheduler object in the constructor of the arrivals class, but I have so many classes referencing each other that doing that for everything would be horrible. There has to be a simple easy way of doing this there usually is.

Thank you
Adam Price
Ranch Hand

Joined: Nov 11, 2005
Posts: 95
Originally posted by Chase Manz:
I will be the first to admit that I am really bad at java and I struggle heavily with it. Our teacher gave us this diagram of how to make our simulation(first my teacher is pretty much useless and impossible to get ahold of).
It has a lot of different classes and all of them reference each other.
For example my Main class creates a CallSystem object, that object creates 3 more objects Arrivals, Conultations, Scheduler. In my Scheduler class there is a variable that keeps track of the time. I need access to the time in the Scheduler object in my Arrivals object. I honestly have no idea how to do this. I tried to pass a reference to the Scheduler object in the constructor of the arrivals class, but I have so many classes referencing each other that doing that for everything would be horrible. There has to be a simple easy way of doing this there usually is.

Thank you


Disclaimer - I am a greenhorn too-

isn't this what getter methods are for? You have a variable for the time in Scheduler object, then you make a getter for it:


then whenever you need the date, you can get it with


Does that not accomplish what you're after?
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3712
    
    5

Encapsulation.

Classes shouldn't be able to access instance variables of other classes directly (at least not for complex data types). Part of the reason you might having so much trouble is that you have too many connections between classes going on at once.

Instead of trying to return one class from within another, figure what the pertinent information you want to know about the instance, and make that a public method. This is called wrapping objects, and it may seem excessive but its very useful in the long run.

Finally, if you need to pass data to a class you create, don't use the constructor. Create a method called setDate() and after creating this object, use this to set the data.

There's a lot more detail I can go into know, and it does suck when you teacher is a waste of time, I'd say you just have to struggle through it until you get a feel for the language, else try a good book.


My Blog: Down Home Country Coding with Scott Selikoff
Chase Manz
Greenhorn

Joined: Dec 08, 2005
Posts: 2
Well thanks for trying to help, but I did not design this, it was designed by who teacher who apparently knows what he is doing. He is in love with diagrams and drawing everything out. It has all of these objects referencing each other and I still have no idea how to do it.


After thinking it some more, maybe I am explaing it wrong. There is one object that keeps track of the time in the simulation. I can make a getTime() method or something to get the time, but to do that I have to create a new object, that doesn't have the current time. I'm really lost on how to make it reference to the one that has the current time. I was thinking maybe of trying to create an array or object that has references to all of the other objects somehow.
[ December 08, 2005: Message edited by: Chase Manz ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Chase Manz:
... I was thinking maybe of trying to create an array or object that has references to all of the other objects somehow.

I think I understand your concern: You're worried about a tangled web of references, and you're considering some sort of "communication hub" to sort things out.

Can you explain more about what classes are required, what exactly they do, and how they need to communicate with each other?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Well, if CallSystem creates instances of Scheduler and Arrivals, and the Arrivals instance needs to get data from the Scheduler instance, then my interpretation is something like this...
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
A web of inter-dependencies like that is usually A Bad Thing because it makes it hard to change one class without breaking another or to reuse one without another. But, if that's what an instructor gives you, you gotta do it. Just this once.

Objects can pass themselves to others in the constructors or you could make a new "assembler" class that does nothing but stitch all the others together.

It's ugly but it's only in one place.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Referencing objects created by other classes.