Results 1 to 8 of 8
  1. #1
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default 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 compiles
    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();
    	}
    	
    }
    This doesn't compile

    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.

  2. #2
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default 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);

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,365
    Blog Entries
    7
    Rep Power
    20

    Default Re: where methods final variables are saved

    Quote Originally Posted by eRaaaa View Post
    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);
    That is not true; the value of a final variable may only be known at runtime, so the compiler cannot treat it as a constant; the compiler generates code to copy the final variable as a member of the local class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default 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
    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();
        }
    }
    the bytecode:
    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
    
    }
    (take a look at the line 3: bipush 10)!!!
    now my assertion (makes no difference if use final int x or only int x)
    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();
        }
    }
    will generate exakt the same bytecode!
    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
    
    }

  5. #5
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default 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?

    makes no difference if use final int x or only int x
    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?

    By the way thanks for the reply guys.
    Last edited by killutch; 10-24-2012 at 04:23 AM.

  6. #6
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: where methods final variables are saved

    Quote Originally Posted by killutch View Post
    Did both my example programs compile for you?
    No your second one not, but my second one is a bit different (I use the 10 at System.out and not the variable x, so here we don`t need the final, but both of them (with final and not) produces the same bytecode here in my second example, not in yours!)

  7. #7
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: where methods final variables are saved

    Quote Originally Posted by eRaaaa View Post
    No your second one not, but my second one is a bit different (I use the 10 at System.out and not the variable x, so here we don`t need the final, but both of them (with final and not) produces the same bytecode here in my second example, not in yours!)
    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.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: where methods final variables are saved

    It's not a 'weird rule,' it's specified: Chapter*8.*Classes
    Any 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.
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Final variables in method
    By noobplus in forum New To Java
    Replies: 2
    Last Post: 03-04-2012, 03:32 PM
  2. Using variables in different methods
    By squirmytoad in forum New To Java
    Replies: 2
    Last Post: 01-10-2012, 08:04 PM
  3. Final variables shouldn't also be static?
    By emilioJazz in forum Advanced Java
    Replies: 5
    Last Post: 11-29-2010, 11:10 AM
  4. final variables in abstract classes
    By parulmahajan in forum New To Java
    Replies: 4
    Last Post: 06-11-2010, 09:54 AM
  5. variables-methods
    By Warren in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 04:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •