Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

JAVA Containing Classes


ghost's Avatar
0 0

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?


ghost's Avatar
0 0

What do you mean "for b to figure out what objA is"? It's kinda hard understanding what you're asking because you seem flustered and more concerned about expressing how you think it's not possible. :p


ghost's Avatar
0 0

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.


ghost's Avatar
0 0

Why would you want to do that? :xx: When you create an instance of objA you can already reference it's methods and variables (depending on privacy level), there would be no need to call it from within objB inside of objA as far as I can see.


ghost's Avatar
0 0

yeah…normally there isnt. but you see i didnt get to create the objA class, only the objB class that is called by objA and i want to access some of its data.


ghost's Avatar
0 0

I'm not even going to ask what you're doing lol. You can try passing a pointer objA as a parameter in the constructor and than from there call the methods.


ghost's Avatar
0 0

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.


ghost's Avatar
0 0

Yeah, it's hard to grasp EXACTLY what you're intending to do. If you have some more elaborate code that you'd like to post, I might e able to help you from there, otherwise, I'm not quite sure right now.


ghost's Avatar
0 0

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.