Results 1 to 5 of 5
Thread: using an instance across classes
- 06-25-2011, 04:12 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
using an instance across classes
Hi everyone!
suppose I have two static classes: main class, aux class and a myobject class
myobject Class:
public class myobject {
public String Name;
public int Size, Status
public myobject() {
// Constructor
Size = 1;
Name = "Ball";
Status = 1;
}
}
Then I have something in the middle of my Main class
.....
myobject puck = new myobject();
puck.Name = "Puck";
puck.Size = 2;
....
Then somewhere down the road I call a method in my aux class
aux.BreakObject(puck);
Then I need in the aux class, a defined method that will effectively do this:L
public void BreakObject(){
main.puck.Status = 0;
}
---
I know the above doesn't work. What can I do to make the object I've created in one class, usable (and changeable) in another?
Thanks.
I.P.
- 06-25-2011, 04:29 AM #2
Here you are passing a reference to myobject in puck to the aux class. You should save that referenceJava Code:aux.BreakObject(puck); // pass a reference to myobject to the aux class
so that you can use it later:Java Code:myobject puckR = puck; // save passed reference to myobject
Since you have the reference to the myobject object, you don't need to worry about a reference to main.Java Code:puckR.Status = 0; // get myobject's Status field by using the saved puck reference
- 06-25-2011, 04:31 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
First you should be using standard Java code conventions: start variables and methods with a lowercase letter and classes with an uppercase one. So, MyObject, name, size, breakObject etc.
To reference an object at all (what I think you mean by having it "usable") you must have a reference to it. And you get this reference by passing it as an argument to the method that wants to use the object. (Alternatively you might pass the reference to the constructor of the class defining the method or to some other method which assigns the reference to a variable so that the method could, once again, get at it.)What can I do to make the object I've created in one class, usable (and changeable) in another?
If you want to alter the state of an object (=="make it changeable") the most straight forward thing to do is to have the class defining the type of that object define a method that makes the appropriate change. You can have something like status public, but it might end up being far easier to have a MyObject declare a setStatus() method.
Java Code:public void breakObject(MyObject obj) { obj.setStatus(0); }
- 06-25-2011, 04:31 AM #4
From your misuse of terminology ('static classes'? really?), total lack of encapsulation (public fields) and the question you asked, I can see you require far more inputs than can be reasonably imparted in a forum thread. Here's a good learning resource for Java: The Java™ Tutorials
Here are two more useful links for getting targeted help in a forum:
How To Ask Questions The Smart Way
SSCCE : Java Glossary
db
- 06-25-2011, 09:30 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Instance variables
By lala in forum New To JavaReplies: 1Last Post: 01-26-2011, 04:38 PM -
Single instance
By anilkumar_vist in forum Advanced JavaReplies: 2Last Post: 08-29-2010, 07:47 PM -
Find instance that owns instance?
By Addez in forum New To JavaReplies: 2Last Post: 04-04-2010, 03:36 PM -
OOP/RTII: Abstract classes with methods using uninitialized instance variables?
By CyJackX in forum New To JavaReplies: 12Last Post: 03-16-2010, 06:45 PM -
New Instance for SWT
By srinivasa_v in forum SWT / JFaceReplies: 1Last Post: 08-08-2007, 01:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks