About Question enthuware.ocpjp.v7.2.1480 :

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

Moderator: admin

Post Reply
simsko
Posts: 4
Joined: Mon Dec 01, 2014 10:52 am
Contact:

About Question enthuware.ocpjp.v7.2.1480 :

Post 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();       }
}

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

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

Post by admin »

I checked your code. It doesn't compile. ai is not accessible.

HTH,
Paul.

simsko
Posts: 4
Joined: Mon Dec 01, 2014 10:52 am
Contact:

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

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

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

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

Post by admin »

I haven't yet looked into Java 8 in detail but I would suggest you to check out the specification.

Svetopolk
Posts: 22
Joined: Sun Nov 18, 2012 1:51 am
Contact:

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

Post by Svetopolk »

"automatic variables" - is it really java terminology? I have never seen it before.

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

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

Post by admin »

Yes, it is not something that we cooked up :)

Chen@ukr.net
Posts: 9
Joined: Sat Feb 27, 2016 1:17 pm
Contact:

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

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

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 5 guests