File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Local Inner Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Local Inner Classes" Watch "Local Inner Classes" New topic
Author

Local Inner Classes

P Hunjan
Greenhorn

Joined: May 20, 2005
Posts: 16
"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

Joined: Aug 31, 2004
Posts: 11343

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...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
P Hunjan
Greenhorn

Joined: May 20, 2005
Posts: 16
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

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
P Hunjan
Greenhorn

Joined: May 20, 2005
Posts: 16
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: May 20, 2005
Posts: 16
Thanks
 
GeeCON Prague 2014
 
subject: Local Inner Classes