Results 1 to 8 of 8
- 10-23-2012, 08:30 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
where methods final variables are saved
Question:
When a final variable is declared with in a method does that get saved on the heap or in a stack?
Reason I Ask:
When dealing with inner class specifically ones inside methods you can't access the methods variables but you can access the class variables accept when the method has a final variable.
Example:
This compilesThis doesn't compileJava Code:public class Test{ public static void main(String[] args){ Test t = new Test(); t.bam(); } public void bam(){ final int x = 10; class Foo{ public void ya(){ System.out.println(x); } } Foo b = new Foo(); b.ya(); } }
Java Code:public class Test{ public static void main(String[] args){ Test t = new Test(); t.bam(); } public void bam(){ int x = 10; class Foo{ public void ya(){ System.out.println(x); } } Foo b = new Foo(); b.ya(); } }Last edited by killutch; 10-23-2012 at 08:34 AM.
- 10-23-2012, 05:48 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: where methods final variables are saved
If you use final variables, you have constants so the compiler can (during compilation) replace the x in your inner class to the constant value (10)
So after compilation you have something like
System.out.println(10); instead of System.out.println(x);
- 10-23-2012, 06:12 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: where methods final variables are saved
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-23-2012, 07:04 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: where methods final variables are saved
Mhmm, I reffered (with "use final variables") to the example of the TO, you too ?(here we have a compile time constant expression, as described in the java language spec or?)
Take a look at the bytecodes!
The original code
the bytecode:Java Code:public class Test{ public static void main(String[] args){ Test t = new Test(); t.bam(); } public void bam(){ final int x = 10; class Foo{ public void ya(){ System.out.println(x); } } Foo b = new Foo(); b.ya(); } }
(take a look at the line 3: bipush 10)!!!Java Code:Compiled from "Test.java" class Test$1Foo extends java.lang.Object{ final Test this$0; Test$1Foo(Test); Code: 0: aload_0 1: aload_1 2: putfield #10; //Field this$0:LTest; 5: aload_0 6: invokespecial #12; //Method java/lang/Object."<init>":()V 9: return public void ya(); Code: 0: getstatic #20; //Field java/lang/System.out:Ljava/io/PrintStream; 3: bipush 10 5: invokevirtual #26; //Method java/io/PrintStream.println:(I)V 8: return }
now my assertion (makes no difference if use final int x or only int x)
will generate exakt the same bytecode!Java Code:public class Test{ public static void main(String[] args){ Test t = new Test(); t.bam(); } public void bam(){ final int x = 10; class Foo{ public void ya(){ System.out.println(10); } } Foo b = new Foo(); b.ya(); } }
Java Code:Compiled from "Test.java" class Test$1Foo extends java.lang.Object{ final Test this$0; Test$1Foo(Test); Code: 0: aload_0 1: aload_1 2: putfield #10; //Field this$0:LTest; 5: aload_0 6: invokespecial #12; //Method java/lang/Object."<init>":()V 9: return public void ya(); Code: 0: getstatic #20; //Field java/lang/System.out:Ljava/io/PrintStream; 3: bipush 10 5: invokevirtual #26; //Method java/io/PrintStream.println:(I)V 8: return }
- 10-24-2012, 03:34 AM #5
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
Re: where methods final variables are saved
So it looks like it is known at compile time but I'm still guessing josha is right about finals getting treated like they where declared on the class level?
I don't know byte code but judging off of what I see your saying they both produce the same byte code? Did both my example programs compile for you?makes no difference if use final int x or only int x
By the way thanks for the reply guys.Last edited by killutch; 10-24-2012 at 04:23 AM.
- 10-24-2012, 07:12 AM #6
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: where methods final variables are saved
- 10-24-2012, 09:21 AM #7
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
Re: where methods final variables are saved
Wow interesting so the java does some tricks at compile time (I'm guessing to increase performance). If you have two programs not equal and you end up with the same byte code but looking into the byte code doesn't explain at all why one can be accessed in a method inner class. I think I'm going to adopt what Josahs said to try and remember this weird rule.
- 10-24-2012, 05:28 PM #8
Re: where methods final variables are saved
It's not a 'weird rule,' it's specified: Chapter*8.*Classes
dbAny local variable, formal parameter, or exception parameter used but not declared in an inner class must be declared final.
Any local variable used but not declared in an inner class must be definitely assigned (§16) before the body of the inner class.Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Final variables in method
By noobplus in forum New To JavaReplies: 2Last Post: 03-04-2012, 03:32 PM -
Using variables in different methods
By squirmytoad in forum New To JavaReplies: 2Last Post: 01-10-2012, 08:04 PM -
Final variables shouldn't also be static?
By emilioJazz in forum Advanced JavaReplies: 5Last Post: 11-29-2010, 11:10 AM -
final variables in abstract classes
By parulmahajan in forum New To JavaReplies: 4Last Post: 06-11-2010, 09:54 AM -
variables-methods
By Warren in forum New To JavaReplies: 1Last Post: 11-28-2007, 04:14 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks