This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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!
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!