About Question enthuware.ocpjp.v7.2.1501 :
Moderator: admin
-
- Posts: 15
- Joined: Tue Jan 13, 2015 12:10 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1501 :
Hi, why automatic variables are not accessible for en inner class defined in a non-static method? Is it any logic back it? Thanks in advance.
Online
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1501 :
I didn't see any special reasoning for it mentioned in the JLS. My guess is that it is because an object of the inner class may survive even after the method ends, while automatic variables are created on the stack, which is cleaned up after the method ends. So it is not really possible to have access to a variable that doesn't exist.
Actually, in Java 8, such variable are accessible in an inner class if they are "effectively final" i.e. if such variables never change after assigned a value. This is possible because now only the value needs to be preserved and not the actual variable.
HTH,
Paul.
Actually, in Java 8, such variable are accessible in an inner class if they are "effectively final" i.e. if such variables never change after assigned a value. This is possible because now only the value needs to be preserved and not the actual variable.
HTH,
Paul.
Who is online
Users browsing this forum: Bing [Bot] and 6 guests