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