*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner Class Question 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 "Inner Class Question" Watch "Inner Class Question" New topic
Author

Inner Class Question

Robert Darling
Ranch Hand

Joined: Jun 21, 2008
Posts: 100

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

Joined: Jun 21, 2008
Posts: 100

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.
bonolo nyoni
Greenhorn

Joined: Nov 19, 2009
Posts: 4

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

Joined: Jun 21, 2008
Posts: 100

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

Joined: Jun 21, 2008
Posts: 100

Is it a holiday at Javaranch :0
Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118
Maybe you need to clarify your question.
Robert Darling
Ranch Hand

Joined: Jun 21, 2008
Posts: 100

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

Joined: Mar 18, 2003
Posts: 118
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.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hi,

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

Best Regards,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
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.


SCJP 6
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi Prithvi,
Best of luck for your course completion.


Cheers Munees
My Blog
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
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,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner Class Question
 
Similar Threads
local inner classes
Method-local inner class & final variables
Doubt in Method Local inner classes
Method local inner classes
method local inner class