File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes needs explanation 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 "needs explanation" Watch "needs explanation" New topic

needs explanation

Venkat Ramsimha
Ranch Hand

Joined: Dec 28, 2004
Posts: 127
class MyOuter2 {
private String x = "Outer2";
void doStuff() {
class MyInner {
public void seeOuter() {
System.out.println("Outer x is " + x);
} // close inner class method
} // close inner class definition
MyInner mi = new MyInner(); // This line must come
// after the class
} // close outer class method doStuff()
} // close outer class

can anybody provide with the explantion as how the output is generated?

marc weber

Joined: Aug 31, 2004
Posts: 11343

Assuming you create an instance of MyOuter2 and call the doStuff() method...
  • Within doStuff(), a new instance of MyInner is created using the local class definition.
  • The method seeOuter() is called on that instance.
  • The method seeOuter() prints a line, accessing MyOuter's private variable x in the process.

  • "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    I agree. Here's the link:
    subject: needs explanation
    It's not a secret anymore!