• 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

method data passing

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so here is what I would like to be able to do:
I have two objects first object creates several instances of second object. Every instance of second object needs to be able to call a method of first object.

I have no idea how to word this question in a google search, or what to call the subject of this post.
-thx
 
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean something like this?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your choices are:
Create instances of the first class and use those to call the methods of the second class (as James shows you). In this case class2 is "composed" of instances of class1.
You could make it a requirement that you pass an instance or more of class1 when you create an instance of class2. In that case class2 would be an agregate of class1's. You would still use the instance of class1 to invoke the methods of class1.
You could have class2 be a subclass of class1, in which case all instances of class2 will inherit all the methods of class1 that are not overridden in class2. (But you say that you are creating many instances of class1 in class2 so this probably is not what you want).
You could make the methods in class1 static, and use the ClassName.staticMethod() syntax to invoke them. See the Math class for a lovely example of this. No one ever creates any little Math instances (because the constructor is private so you can't), but you can still use all the static methods whenever you want. (again probably not what you are talking about).
You could have class2 be an inner class of class1, in which case the instances of class2 can use all the methods of class1.
[ March 04, 2003: Message edited by: Cindy Glass ]
 
Jeremiah Elliott
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Guys, got it working.
 
reply
    Bookmark Topic Watch Topic
  • New Topic