Results 1 to 5 of 5
  1. #1
    ipounder is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default 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.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default

    Java Code:
    aux.BreakObject(puck);  // pass a reference to myobject to the aux class
    Here you are passing a reference to myobject in puck to the aux class. You should save that reference
    Java Code:
    myobject puckR = puck;  // save passed reference to myobject
    so that you can use it later:
    Java Code:
    puckR.Status = 0;   // get myobject's Status field by using the saved puck reference
    Since you have the reference to the myobject object, you don't need to worry about a reference to main.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

    What can I do to make the object I've created in one class, usable (and changeable) in another?
    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.)

    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);
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default

    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

  5. #5
    ipounder is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Java Code:
    aux.BreakObject(puck);  // pass a reference to myobject to the aux class
    Here you are passing a reference to myobject in puck to the aux class. You should save that reference
    Java Code:
    myobject puckR = puck;  // save passed reference to myobject
    so that you can use it later:
    Java Code:
    puckR.Status = 0;   // get myobject's Status field by using the saved puck reference
    Since you have the reference to the myobject object, you don't need to worry about a reference to main.
    Thanks!

    This is exactly what I was missing!!!!

Similar Threads

  1. Instance variables
    By lala in forum New To Java
    Replies: 1
    Last Post: 01-26-2011, 04:38 PM
  2. Single instance
    By anilkumar_vist in forum Advanced Java
    Replies: 2
    Last Post: 08-29-2010, 07:47 PM
  3. Find instance that owns instance?
    By Addez in forum New To Java
    Replies: 2
    Last Post: 04-04-2010, 03:36 PM
  4. Replies: 12
    Last Post: 03-16-2010, 06:45 PM
  5. New Instance for SWT
    By srinivasa_v in forum SWT / JFace
    Replies: 1
    Last Post: 08-08-2007, 01:02 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •