Page 1 of 1

About Question enthuware.ocpjp.v7.2.1480 :

Posted: Tue Dec 02, 2014 11:07 am
by simsko
From the explanation of the answer on this question, it says "Since method inner() is a static method, only si and fai are accessible in class Inner. Note that ai and ii are not accessible. If method inner() were a non - static method, ii would have been accessible." However, I am able to compile and run the code below without any exceptions, where ai now IS accessible. Any thoughts on why? Is the answer wrong?

Code: Select all

public class TestClass {
   static int si = 10;   int ii = 20;
   public static void inner() {
      int ai = 30; //automatic variable
      final int fai = 40; //automatic final variable
      class Inner {
          public Inner()  {   System.out.println(ai + "  " + fai);      }
      }
      new Inner();
   }
   public static void main(String[] args)   {     TestClass.inner();       }
}

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

Posted: Tue Dec 02, 2014 8:48 pm
by admin
I checked your code. It doesn't compile. ai is not accessible.

HTH,
Paul.

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

Posted: Wed Dec 03, 2014 10:51 am
by simsko
Hi Paul, thanks for answering. I found the problem being that I used jdk 1.8, not 1.7, so once I switched over it wouldn't compile any longer. Is there any specific reason to why this is allowed in java 8?

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

Posted: Wed Dec 03, 2014 11:12 am
by admin
I haven't yet looked into Java 8 in detail but I would suggest you to check out the specification.

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

Posted: Sat Jan 03, 2015 3:18 pm
by Svetopolk
"automatic variables" - is it really java terminology? I have never seen it before.

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

Posted: Sat Jan 03, 2015 9:32 pm
by admin
Yes, it is not something that we cooked up :)

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

Posted: Sat Apr 09, 2016 12:55 am
by Chen@ukr.net
The most hard part of question was that
All final automatic variables

Code: Select all

public class TestClass
{
   static int si = 10;   int ii = 20;
   public static void inner()
   {
      int ai = 30; //automatic variable
      final int fai = 40; //automatic final variable
      class Inner
      {
          public Inner()  {   System.out.println(si+"    "+fai);      }
      }
      new Inner();
   }
   public static void main(String[] args)   {     TestClass.inner();       }
    public static void OtherInnerMethod(){
             final int otherFai = 40; // other automatic final variable
    }
}
I add other static method and add other automatic final variable.
otherFai can`t be accessed by Inner class.

also i can add declaration of final automation variable after declaration of Inner class.

All instance variables - can be accessed if i create instance of outer class, but not All automatic final variable can be accessed

How can i resolve such question?