• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner class passing reference of outer class  RSS feed

 
Hans Stoeber
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
suppose I have a class which contains a public, non-static inner class. The inner class has a hidden this reference to the outer class by default, so I can access methods and members. Question: Is it possible to pass the instance of the outer class in the inner class to some other method?
Example:
public class Outer
{
public class Inner
{
public void innerMethod()
{
MyClass.someMethodAcceptingOuterInstance(???);
}
}
}
public class MyClass
{
public static void someMethodAcceptingOuterInstance(Outer outer)
{
...
}
}
I just cannot get this code to compile, any hints?
Thanks,
Jochen
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jochen,
Welcome to JavaRanch. The syntax for accessing the outer class is Outer.this
 
Hans Stoeber
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,
thanks very much for your answer!
Jochen
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!