About Question enthuware.ocpjp.v7.2.1501 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Alina_Lapina
Posts: 15
Joined: Tue Jan 13, 2015 12:10 pm
Contact:

About Question enthuware.ocpjp.v7.2.1501 :

Post by Alina_Lapina »

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.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1501 :

Post by admin »

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests