Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing enclosing class method from inner class

 
richard rehl
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A question: in the following code, taken from SB6 Chapter 8 Self Test, I understand how to access the enclosing class method drive() from the inner class constructor and the initializer block.
but if I make a new instance of the enclosing class from within the inner class, I'm confused as to why I can't call the same method using that reference.
 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It will work if you call it from some block or method. Whether its an inner class or whatever you cannot call methods in a class where you can make only declarations or define methods.
I have added a works method and called c.drive() from there.

 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What I mean is you cannot do this -

class A{

B bb = new B();
bb.go(); // THIS IS WRONG

}

class B{

void go(){}

}


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic