• 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

Inner Class Question

 
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi i have a small problem regarding method local inner classes. Here is some code


Now i understand that if x, that is declared in the fuction is not marked final, it will not be accesible to the class declared inside the method. it has to be marked final. The reason being that x will be stored on the stack and when the fucntion ends the stack gets blown away. But the thing i cannot understand is


Would this line of code not also put the inner variable on the same stack as the variable x, and since they are on the same stack, why mark them final.

Secondly whats the deal with final? How does marking a variable final increase its lifetime. Its still on the stack right, or is it shifted to the heap ?


 
Robert Darling
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also could you tell me what it means for an inner level class to be abstract ? Why would we want to mark an inner level class abstract ? Also can a method level inner class inherit from classes out the class in which it has been defined ? What are the rules to this.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

after you compile the above nested class you get two classes named
1.doStaffClass.class
2.doStaffClass$1functionInner.class
A complier generates top- level classes from any nested classes. for the variable access scope to be available to a top-level class it must be final.all local variables defined as final are copied to the Top-level class during compiling process.
 
Robert Darling
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly does this mean. If i declare a variable as final in a method it be accesible outside the method ?
 
Robert Darling
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it a holiday at Javaranch :0
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you need to clarify your question.
 
Robert Darling
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question in asked is why do we have to mark a variable final if we want to access it from a method local class. Both would or should be defined on a stack. Secondly i don't understand how marking a variable final increases its life.
 
Jonas Isberg
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sajjad Dar wrote:why do we have to mark a variable final if we want to access it from a method local class.


As you say

Sajjad Dar wrote:x will be stored on the stack and when the fucntion ends the stack gets blown away.



Sajjad Dar wrote: Both would or should be defined on a stack.


Neither the inner class nor the instance of the inner class will be stored on the stack (the reference variable will though).
The instance will be stored on the heap and might very well exist after the function that creates it ends.

Sajjad Dar wrote:Secondly i don't understand how marking a variable final increases its life.


It becomes a constant, and therefore it is perfectly safe for java to make a copy of it and use inside the inner class.
It is as the inner class gets its very own x.
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Seems i am on a holiday till 5th of May till i don't complete my course
Will only ask doubts

Best Regards,
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prithvi Sehgal wrote:Hi,

Seems i am on a holiday till 5th of May till i don't complete my course
Will only ask doubts

Best Regards,

hi Prithvi, Good luck with holiday and the course completion.

Sajjad Dar wrote: Also could you tell me what it means for an inner level class to be abstract ? Why would we want to mark an inner level class abstract ? Also can a method level inner class inherit from classes out the class in which it has been defined ? What are the rules to this.

Those are interesting questions. An abstract inner class means the same as any other kind of abstract class. It can serve as a parent for various other inner classes. The two modifiers, "abstract" and "final" are the only two modifiers allowed for method level inner classes. The other modifiers: "private, "public", "protected", "static", "volatile", and "transient" doesn't make sense for method local variables and for the same reason can't be applied to method level inner classes. That leaves only the "abstract" and "final" modifiers that make sense for those inner classes.

Yes, a method level inner class can inherit from classes outside of the class in which it has been defined. The same rules of inheritance applies to all classes.
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prithvi,
Best of luck for your course completion.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Larry/Munee,

Thank you very much. I am missing javaranch though, will be back in 4-5 days time in full bloom.

Best Regards,
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic