Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Local Inner Classes

 
P Hunjan
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Local inner classes :
This type of inner class is defined inside a bloc of code. However, like any object they can live beyond the scope of the bloc of code inside which they are defined, this depends on what you do with them."
--------------------------------------------------------------------------
I am trying to write code where i can use a Local inner class's variable outside the scope of its block of code. But no luck
Can anyone send me an example?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't use a variable outside of its scope.

If you want to use a variable's value outside of that variable's scope, then you can assign that value to some other variable that is defined outside that scope...
 
P Hunjan
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
I guess i got confused with an Object's life & a variable's scope.
I modified my code which I was trying but I am not getting the desired answer, rather theres something new i found->

Initially I tried this;

Output is 0 rather than 50
------------------------------------------------------------

When I modify it to->


This time output is 50 for both the variables.


( tags added and formatted)
[ July 18, 2005: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you mean local inner class' variable?
You cannot access a variable from such a class directly, but you can use a method:

[ July 18, 2005: Message edited by: Barry Gaunt ]
 
P Hunjan
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks

yes,I meant the local inner class variable.I got confused by coming across the same statement in different reading materials that "An object can live beyond the scope of the bloc of code inside which they are defined". So I tried to do that but in a wrong way.

It clarifies, scope dies but not the object & we can use it in different way. It will be really great if i can get an answer to my another query i have stated regarding different outputs.

P Hunjan
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by P Hunjan:
... It will be really great if i can get an answer to my another query i have stated regarding different outputs...

In your first example, the method is never called, so var is simply left with its default value of 0. If you just add a line to call the method...

outer1.amethod();

...then the output will be 50.
 
P Hunjan
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic