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:
Code:
public Path path[] = new Path[25];
And my use in Class B:
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?
Re: Access object created in another class
Quote:
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:
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?
Re: Access object created in another class
Quote:
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.
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)
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:
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?
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:
Code:
public class Board {
private Path[] path;
public Board() {
path = new Path[25];
path[0]= new Path(1,0);
// etc etc
}
}
Re: Access object created in another class
Quote:
Originally Posted by
Tolls
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:
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!!!