• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Local Inner Classes

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All
How can a local inner class outlive its scope?
This seems to be the reason most texts give for the final modifier requirement when a local inner class is involved.
Thanks in advance.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can define a variable outside of the method, then instantiate it with an object in a method. The method ends, the object is still referenced.
You could return the object at the end of the method, so that something still references it. etc.
 
Zahid, Butt
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Thanks for your answer.
Nut it is still not 100% clear, can you write some code to
illustrate this Please.
Thanks in advance.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I may not be very correct at syntax, but the following pseudo example may help you.
Suppose you have an interface named IShape and your local class, Rectangle, implements this interface. Now your method can create an object of type Rectangle and return it as IShape object.
See this:
interface IShape {
public void draw();
}

class Test {
public IShape create () {
class Rectangle implements IShape {
public void draw () {
System.out.println ("I am draw of rectangle.");
}
}
return new Rectangle();
}
}
public class Main {
public static void main (String args[]) {
IShape i = new Test().create();
i.draw();
}
}
Hope this helps...
Shah.
 
Zahid, Butt
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Thanks for your answer Muhammad.
How come the Compilor does not flag an error ??
Why is this possible ???
Is it because its an object, if so when do objects die in a program if you dont EXPLICITLY command it to ??
Thanks in advance.
 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Butt.
I think (I am still learning this stuff)
If you assign null to the object, it is eligible for garbage collection.
You never know, when the garbage collector will run.
The memory of the object is set free, when the garbage collector runs.
hope this is correct
Axel
 
Did you just should on me? You should read this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic