• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Question 4 Method-Local Inner Class

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dealer ranchers
The puzzle is in below.
The Method-Local Inner Class can't use the local variable which is not marked by final. And one explanation is that the Method-Local Inner Class which is on the heap and the variable which is on the stack have different scope. But if the local variable is marked by fianl, it is still on the stack, isn't it. Why this is valid.
Can you explain it 4 me. Thanks a lot.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check this
https://coderanch.com/t/407046/java/java/where-final-static-variable-stored

https://coderanch.com/t/266525/java-programmer-SCJP/certification/final-variable-final-int-stack
[ April 01, 2008: Message edited by: Justin Russo ]
 
yu yong
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to Justin's link. But I still have some puzzles. That is my explanation in below.

method-local inner class is on heap and the final variable is on heap(the permanent generation of the heap) also. But the references of them are both on the stack. Specifically the final variable's reference is the copy of which is on the heap. So the final variable and the method-local inner class have the same scope.

That is my explanation after reading your link. Is this correct or not.
[ April 01, 2008: Message edited by: yu yong ]
 
Justin Russo
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically all local variables(final or non-final) are on the stack.. nothing is on the heap (except Objects). By local variables i guess we all know that we are talking about primitive types.

I wrote a small program to prove this




Now i compiled it using javac.

To see what the compiler internally did i used the javap tool
Bacially the compiler generated two classes
1. Test.class
2. Test$1Inner.class

javap -c Test
javap -c Test$1Inner

I got the following

This is for outer class
Compiled from "Test.java"
public class Test extends java.lang.Object{
public Test();
Code:
0:aload_0
1:invokespecial#1; //Method java/lang/Object."<init>" )V
4:return

public static void main(java.lang.String[]);
Code:
0:return


This is for Inner class

Compiled from "Test.java"
class Test$1Inner extends java.lang.Object{
Test$1Inner();
Code:
0:aload_0
1:invokespecial#1; //Method java/lang/Object."<init>" )V
4:getstatic#2; //Field java/lang/System.out:Ljava/io/PrintStream;
7:bipush100
9:invokevirtual#3; //Method java/io/PrintStream.println I)V
12:return




Just see bold bytecode operation above. The compiler resolves the final local variable in the Inner Class at compilation time as it knows that this variable is not gonna changes. its immutable.



Hope this clears things now. Reiterating that all local (final or non-final are created on the stack.)
 
yu yong
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to Justin.
I think i have known it.
The method's local final variable is as the same as its member variable which is marked by final. It's just the local variable's copy.
Is this correct.
 
Justin Russo
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes
 
yu yong
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I understood. Thanks a lot.
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you little elaborate on this
[ April 03, 2008: Message edited by: Dinesh Tahiliani ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic