Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By jim829

Thread: Why can local inner classes access 'final' local variables?

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Why can local inner classes access 'final' local variables?

    Local inner classes can't access local variables unless they are declared final.

    It is because local variables are created on the stack and the frame of the method is lost once the method returns. Objects of local inner class would be created on the heap, which will outlive the method's life if a reference to the object was passed outside its parent method (like to some other method etc.) and saved somewhere. So objects of local inner classes can't be allowed to use something which might not exist at some point when the object is still alive.

    This part is understandable.

    What I don't understand is the connection between this and the local variable being final; even if the local variable is marked final, it will still be created and live on the stack right?

    So what is it about a final variable which lets it, probably outlive the method in which it is created?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Why can local inner classes access 'final' local variables?

    Quote Originally Posted by Zarah View Post
    Local inner classes can't access local variables unless they are declared final.
    You need to provide an example of what you mean because I don't agree with the statement as written.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: Why can local inner classes access 'final' local variables?

    Quote Originally Posted by jim829 View Post
    You need to provide an example of what you mean because I don't agree with the statement as written.

    Regards,
    Jim
    Java Code:
    package nested_classes_one.local_inner_classes;
    
    public class TestClass {
    	
    	
    	public static void main(String [] args) {
    		int num = 5;
    		final int numOne = 25;
    		
    		class LocalInnerClass {
    			private int innerNum = num;//****************Cannot refer to the non-final local variable num defined in an enclosing scope
    			private int innerNumOne = numOne;
    		}
    	}
    
    }
    The compiler complains on the second last statement, "Cannot refer to the non-final local variable num defined in an enclosing scope".

    EDIT:

    Sorry it was probably the ambiguous 'they' in my statement which makes you disagree. By 'they', I mean the local variables, not the local classes.
    Last edited by Zarah; 05-22-2016 at 03:54 AM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Why can local inner classes access 'final' local variables?

    It was my lack of understanding that was the problem. But I believe you touched on the correct
    answer. If the local variable was allowed to change then which value would the class use once
    the method returned and the local value went out of scope? A thread on SO said the value was
    copied to the class which is what required it to be declared final. However, I have not found any official
    documentation to support this.

    Thus far I have never used this particular construct. I had thought that your statement was wrong
    because my own code and your code did not flag an error. That is because Java 8 allows effectively
    final values and I misinterpreted my lack of a compile time error. Mea Culpa.

    Regards,
    Jim
    Last edited by jim829; 05-22-2016 at 06:11 AM.
    Zarah likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Why can local inner classes access 'final' local variables?

    You normally only see this with anonymous inner classes"

    Java Code:
    final int localVariable = 12;
    
    someMethodWithCallback(new SomeCallback() {
        public void callback() {
            if (localVariable == 10) {
            }
        }
    });
    ETA: Of course these days that would be a lambda, I expect.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Local variables in java
    By sreekanthgude in forum New To Java
    Replies: 3
    Last Post: 01-11-2014, 03:50 PM
  2. Variables in MainMethod(). Local or Instance.
    By Flamey in forum New To Java
    Replies: 3
    Last Post: 06-10-2011, 06:46 AM
  3. Replies: 1
    Last Post: 03-05-2011, 05:47 PM
  4. Replies: 2
    Last Post: 12-05-2009, 11:24 AM
  5. Local variables unavailable
    By Onra in forum New To Java
    Replies: 9
    Last Post: 11-25-2009, 09:39 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
  •