This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Code for Method-Local Inner Classes not compiling  RSS feed

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 310
1
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 310
1
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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();
 
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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...
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!