This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Code for Method-Local Inner Classes not compiling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Code for Method-Local Inner Classes not compiling" Watch "Code for Method-Local Inner Classes not compiling" New topic
Author

Code for Method-Local Inner Classes not compiling

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Method-Local Inner Classes, Chapter 8, Sierra/Bates:



What is wrong with my code? The compiler outputs:

MyOuter2.java:19: cannot find symbol
symbol : class MyInner
location: class MyOuter2
MyOuter2.MyInner inner = new MyOuter2().new MyInner();
^
MyOuter2.java:19: cannot find symbol
symbol : class MyInner
location: class MyOuter2
MyOuter2.MyInner inner = new MyOuter2().new MyInner();
^
2 errors


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

You cannot access a method local inner class out the method it is defined. As the name suggests, it is kind of local variable to the method and the scope of inner class is limited within the method. However you can access the method like below:



SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Hi Rajeev,

I tried your code while compiles and runs; how do I get seeOuter() to execute?

I've tried several options, but none of them work, please assist.
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

Sandra Bachan wrote:Hi Rajeev,

I tried your code while compiles and runs; how do I get seeOuter() to execute?

I've tried several options, but none of them work, please assist.
Hi Sandra, please see my code. While calling new MyOuter2().doStuff(), doStuff() will call seeOuter();


Otherwise you could call
inside doStuff()


Try to run my code, it gives output of seeOuter();
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Sandra Bachan wrote:Hi Rajeev,

I tried your code while compiles and runs; how do I get seeOuter() to execute?

I've tried several options, but none of them work, please assist.


Try the below code...


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
@ Rajeev: Tried your code, it now works.

@ Abimaran: Tried your code, and now understand more about inner classes. Ran the code with MyOuter obj = new MyOuter2().doStuff(); and then replaced it with MyOuter obj = new MyOuter(); and saw the difference in output.

Thanks!
 
Don't get me started about those stupid light bulbs.
 
subject: Code for Method-Local Inner Classes not compiling
 
Similar Threads
Method Local Inner classes
where final variable reside ?
Doubts regarding Method local inner class
Doubt in Method Local inner classes
Method-local inner class and final local variable