Results 1 to 6 of 6
  1. #1
    BalintD is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Access object created in another class

    I have created an object in one of my classes but I need to be able to access it also in another class. I have spent hours trying to figure it out at no avail. Here is my declaration in Class A:

    Java Code:
    public Path path[] = new Path[25];
    And my use in Class B:

    Java Code:
    x = path[counter].x;
            y = path[counter].y;
    It tells me variable path doesn't exist. I was under the impression when you create and object the whole program can see it. What can I do to fix this?

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Access object created in another class

    I was under the impression when you create and object the whole program can see it.
    Nope! Only if the object is in the same scope. Same scope would be if the object was declared in the same code block, or in an outer code block. If you want objects to be passed back and forth between sibling or distantly related classes, then you need to pass references to the object back and forth. Here is a simple example using accessor methods; I'll use 3 classes: a main class, and two classes which will talk to each other:

    Java Code:
    A.java:
    
    public class A{
        private B myB;
    
        public void printB(){
            System.out.println(myB.getWord());
        }
    
        public void referenceB(B myB){
            this.myB = myB;
        }
    }
    
    B.java:
    
    public class B{
        private String word = "banana";
    
        public int getWord(){
            return word;
        }
    }
    
    Main.java:
    
    public class Main{
        public static void main(String[] args){
            new Main();
        }
    
        public Main(){
            A a = new A();   //make an 'A'
            B b = new B();   //make a 'B'
            a.referenceB(b); //Send a reference of our 'B' to a
            a.printB();         //tell our 'A' to do something with b
        }
    }
    In this example, we pass a reference from one class to another. The first class can now use things that belong to the second (A uses B) because we can 'see' it. We've saved a reference to it called myB. 'myB' in the A class and 'b' in the main class are the same object. Doing things to 'myB' is the same as doing them to 'b'.

    Does that help?

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

    Default Re: Access object created in another class

    I was under the impression when you create and object the whole program can see it.
    Assuming that path was declared as an instance variable of the first class, then other classes can get at it. But they require an instance of the first class in order to do so. That's because path is a path of that instance.

    Java Code:
    class A {
        public Path path = new Path[25];
    } 
    
    class B {
        private int counter;
    
        void method() {
            A a = new A();  // <-- this line, or something like it, is needed
            Thing x = a.path[counter].x;
            Thing y = a.path[counter].y;
        }
    }
    Of course the line constructing the new A() need not occur in the method that uses it: it might be constructed elsewhere in class B, or passed to the class B constructor. But the bottom line is that if path is declared as shown in class A then it must be accessed via an instance of A.

    -----

    Rather a lot of guesswork here. If it doesn't help post the code that gives the error. (Or a compilable, but cut down version of reasonable length)

  4. #4
    BalintD is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: Access object created in another class

    Wow, thank you for all the help and detailed explanations. I apologize for such a late reply. I have tried the first implementation by using references and I have gotten stuck at an error. My codes so far looks like:

    Java Code:
    public class Board extends JPanel implements ActionListener {
    
    public Board() {
    Path path[] = new Path[25];
    path[0]= new Path(1,0);
    path[1]= new Path(5,0);
    path[2]= new Path(15,0);
    .....
    }
    
    public void actionPerformed(ActionEvent e) {
             if(e.getSource() == b2){
           craft.referencePath(path); <------------------------ this part 
            }
          ...
        }
    
    }
    
    
    public class Craft {
    
        private Path path[];
    
        public void referencePath(Path path[]){
            this.path = path;
        }
    
    }
    The arrow part gives me the error: cannot find symbol - variable path. Could it be because path is an array of objects rather than one variable?

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

    Default Re: Access object created in another class

    No.
    It's because path is declared in the constructor of Board, so it only exists in that constructor.
    If you want that path (which is not the same as the path declared in Craft, note) to be visible elsewhere in Board then you'll have to make it an attribute of Board:
    Java Code:
    public class Board {
        private Path[] path;
    
        public Board() {
            path = new Path[25];
            path[0]= new Path(1,0);
            // etc etc
        }
    }

  6. #6
    BalintD is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: Access object created in another class

    Quote Originally Posted by Tolls View Post
    No.
    It's because path is declared in the constructor of Board, so it only exists in that constructor.
    If you want that path (which is not the same as the path declared in Craft, note) to be visible elsewhere in Board then you'll have to make it an attribute of Board:
    Java Code:
    public class Board {
        private Path[] path;
    
        public Board() {
            path = new Path[25];
            path[0]= new Path(1,0);
            // etc etc
        }
    }
    Ahh, I see. Thank you very much!!!

Similar Threads

  1. Replies: 3
    Last Post: 08-25-2011, 04:46 PM
  2. Replies: 2
    Last Post: 06-16-2011, 09:39 AM
  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
  •