• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Referencing objects created by other classes.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Chase Manz
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic