• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Local Inner Classes

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic