JAVA Containing Classes
Alright, I'm actually pretty sure this is not implemented into java, and not really sure if its even possible, but does anyone know of a way to get the containing object for something your object is in? i.e.:
public class objA{ public objA() { objB b = new objB(); } }//end of objA class
public class objB{ public objB() { } }//end of objB class
i want some way for b to figure out what objA is. So the memory location or even a returnable object would be great. I have a piece of code for a project and it would be great if I could get some of the data out of that other object. Again, really pretty sure you can't do this….but still…any ideas?
haha….sorry. okay…say that object A has a bunch of other vars in it along with objB right? i want to be able to see what those are. I know i cant access back up, things work top down. But i was thinking maybe there was a way to nab the memory location or somehting like that. I need to be able to read those.
hah….again cant do that because i cant modify the call to objB. lemme explain what im trying todo.
i have a cs lab and we're writing the "ai" portion for a battleship game. so my "ai" object gets called by the battleship class. i want to access its data so i dont have to guess where this guy has hidden his ship, i can know. I've already written a really nice "ai" so im set on that. Someone in the class just asked me if i knew any way to do that and i started thinking.
Alright, I think I understand what you are trying to do. Hopefully this bit of code I just wrote up for you will help..
objA.java
public static String str;
public objA() {
str = new String("Hello World");
objB b = new objB();
}
public static void main(String args[]) {
objA ob = new objA();
}
}```
objB.java
```markuppublic class objB {
public objB() {
System.out.println(objA.str);
}
}```
Output:
```markupHello World
Press any key to continue . . .```
If this is not what you want just let me know, but I am pretty sure this is what you want. It is pretty self explanitory on what its doing, but if you want me to comment it up just let me know, or I can just explain it in this thread.