File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Accessing enclosing class method from inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Accessing enclosing class method from inner class" Watch "Accessing enclosing class method from inner class" New topic
Author

Accessing enclosing class method from inner class

richard rehl
Ranch Hand

Joined: May 21, 2007
Posts: 36
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

Joined: Jan 09, 2010
Posts: 183

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

Joined: Jan 09, 2010
Posts: 183


What I mean is you cannot do this -

class A{

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

}

class B{

void go(){}

}


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing enclosing class method from inner class