Results 1 to 4 of 4
  1. #1
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default Conceptual question: access to an object created in another class

    I hope you will forgive me for not posting the code since I think this is a conceptual problem. In my main class I have created an object, which has its own variables of a particular value. Then in another class which handles the GUI of my program I wish to refer to this object and its values. I do not know how to do this. I can't create a new object of the same class since this would not share the same values as the object in the main class. Can anyone set my thinking straight?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default

    You're going to have to pass the callee Object to the caller Object, either via a set method or by passing it into the constructor.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    Pass the first object into the second, either in the constructor or a set method.
    Java Code:
    public class Main {
        .... main(String[] args) {
            SomeClass someObject = new SomeClass();
            MyGUI myGUI = new MyGUI(someObject);
    ....
        }
    }
    
    ...
    public class MyGUI {
        SomeClass someObject;
        public MyGUI(SomeClass someObject) {
            this.someObject = someObject;
        }
    }
    Something along those lines.

  4. #4
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 06-16-2011, 09:39 AM
  2. Class object passed into println(obj) question
    By silverglade in forum New To Java
    Replies: 4
    Last Post: 05-15-2011, 11:27 PM
  3. Replies: 3
    Last Post: 05-02-2011, 04:37 AM
  4. Replies: 2
    Last Post: 02-18-2010, 05:26 PM
  5. Replies: 1
    Last Post: 03-04-2009, 06:14 PM

Posting Permissions

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