Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reflectively obtain an inner object's outer object

807569Aug 11 2006 — edited Aug 11 2006
So, I have an object which is an instance of an inner class. I need to find reflectively the instance of the outer class which is associated with it.

I might not be seeing the wood for the trees here but I can't see how. Effectively I need an object equivalent of "Class.getDeclaringClass()"

An example to help explain, if required:
public class FindOuterObject
{
	public static void main(String[] args)
	{
		new FindOuterObject();
	}
	
	private FindOuterObject()
	{
		InnerObject i = new InnerObject();
		Object o = findOuterObject(i);
		System.out.println((o == this) ? "Worked" : "Failed");
	}
	
	private static Object findOuterObject(Object innerObject)
	{
		// Need to fill in this method to work for any object passed in
		return null;
	}

	public class InnerObject
	{
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 8 2006
Added on Aug 11 2006
8 comments
234 views