• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Accessing the object X by a method of object Y (Y instantiated inside X)

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
To all java gurus all over the world. I need a solution to a problem urgently.Please go through the code first:

The two classes are in separate files.When I run the second class after compilation, the aMehtod() of the second class will be invoked. Now in the aMethod of the second class, I want a reference of the first class. To be precise, inside the mc.aMehtod(), I want a reference of CCObj object of class ContainerClass.
I don't want to pass the object reference of CCObj as a parameter to aMethod. Moreover, I don't want only the reference to CCObj, but any other object of any other class for that matter, inside which I instantiate an object of class MyClass.
All I know is that the reference is obtained at runtime dynamically and some kind of reflection concept is also to be used.
Can anyone tell me how do I obtain the reference?
regards
Tanveer
 
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um, there are ways, but they're not good OO. Can you give us the problem to which oyu believe this is the solution?
One approach is to use static variables, and set the static variable as an objetc is created. Since you have a variable number of objects, you can create a public static vector in MyClass, and after every constructor call, an object gets added to this vector.
There are other variations on this theme, but all generally bad OO design.

--Mark
hershey@vaultus.com
 
Tanveer Rameez
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi to Mark and to all java gurus

It is not very easy to explain why I need it. I am in a project where I have to develop components which will be inserted by the client into frames/applets with the help of the JBuilder 4.0. The component (in this case MyClass, from the code above) will be visible in the component bar of the design panel. The client can simply drag the component(say MyClass) from the component bar into the frame, and an object of that component is automatically instantiated(a default constructor is called by the code written by JBuilder itself).
Now if he invokes any method(say aMethod()) of the MyClass object, the MyClass Object must get the reference of the Frame object inside which it is instantiated and perform some operations on the Frame object.
We could have solved this problem by passing the reference of Frame object through the constructor or aMethod(). But since the default constructor is called by JBuilder itself and the client doesn't want to pass the reference through the aMethod, the MyClass object should get the reference of the frame object on its own.
You can get the reference of the Frame object inside another GUI component(Button,JPanel etc) if that component is a member variable of that Frame, by the getParent() method for that added component. I want something like that for a non GUI component(MyClass).
I hope you understand the problem.
regards
Tanveer
 
Tanveer Rameez
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi to Mark and to all java gurus

It is not very easy to explain why I need it. I am in a project where I have to develop components which will be inserted by the client into frames/applets with the help of the JBuilder 4.0. The component (in this case MyClass, from the code above) will be visible in the component bar of the design panel. The client can simply drag the component(say MyClass) from the component bar into the frame, and an object of that component is automatically instantiated(a default constructor is called by the code written by JBuilder itself).
Now if he invokes any method(say aMethod()) of the MyClass object, the MyClass Object must get the reference of the Frame object inside which it is instantiated and perform some operations on the Frame object.
We could have solved this problem by passing the reference of Frame object through the constructor or aMethod(). But since the default constructor is called by JBuilder itself and the client doesn't want to pass the reference through the aMethod, the MyClass object should get the reference of the frame object on its own.
You can get the reference of the Frame object inside another GUI component(Button,JPanel etc) if that component is a member variable of that Frame, by the getParent() method for that added component. I want something like that for a non GUI component(MyClass).
I hope you understand the problem.
regards
Tanveer
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you have described is a Java Bean, being used at design time in an IDE. What you are asking for is a way that a Java Bean can be developed, with no knowledge of its use, and yet know something about the enviornment in which it is used. If it's not GUI, I think you're out of luck.

Consider using the IDE's text editor to modify the constructor code after the IDE writes the line.
Also consider redesigning the system so it does not require this funtionality.

--Mark
hershey@vaultus.com
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!