Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Using object in different class 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 "Using object in different class" Watch "Using object in different class" New topic
Author

Using object in different class

Jackie Davis
Greenhorn

Joined: Mar 04, 2006
Posts: 23
Hi there, another concept Im struggling with...







I have methods defined in a Mesh class. I want to use these methods as the scrollbar is adjusted - (i.e. within the ScrollbarListener class - geo.someMethod() ) However geo is inialised in the MeshTesterGLSpace class. How can I use this object (geo) in the ScrollbarListener class?
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
ScrollListener, being an inner class of MeshTester, can invoke any method on its enclosing object, even private methods:

Therefore, if you can write a MeshTester method that does what you want, then you can call it from ScrollListener.


There is no emoticon for what I am feeling!
Jackie Davis
Greenhorn

Joined: Mar 04, 2006
Posts: 23
Hmmm...

I see what you are saying. But I have a 'Mesh' object intialised in the MeshTesterGLSpace class called 'geo'.

I have a seperate Mesh class with methods.

I would like to use the object geo in ScrollListener (i.e geo.getVertices(); ). But because the Mesh object 'geo' has not been initalised in the MeshTester class I cannot use it here.

Hope I've explained this ok.

How do I get around it?

Thanks
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
You metioned that field geo has not been initialised. Why not? It's hard to send a message to an object that doesn't exist
Vinayak patil
Ranch Hand

Joined: Aug 30, 2005
Posts: 67
This may help:

Have a MeshTesterGLSpace instance (myMeshTesterGLSpace) in the MeshTester Class.

Call the method that initializes the geo object:
myMeshTesterGLSpace.initMesh()

Then you can use the geo object as

myMeshTesterGLSpace.geo

-Vinayak


"I can resist everything except temptation"
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Also, what's the relationship between MeshTester and MeshTesterGLSpace?
1. Does one have an instance of the other as a field?
2. Is one passed the other in some method?
3. Is one created locally in the method of the other?
There's many ways to get objects to send messages to each other.
 
 
subject: Using object in different class