About Question com.enthuware.ets.scjp.v6.2.703

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

Moderator: admin

Post Reply
jbprek
Posts: 4
Joined: Sat Aug 30, 2014 4:19 am
Contact:

About Question com.enthuware.ets.scjp.v6.2.703

Post by jbprek »

Can you please advise on the following:
Why do we have to select only option 5 "All of these are valid.", instead of selecting all 5 options, since the question asks to select all valid options?
Is this the recommended approach to follow in real exam?

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

Re: About Question com.enthuware.ets.scjp.v6.2.703

Post by admin »

The real exam tells you the exact number of options to select. For example, in this question you will be asked to select exactly one option. That will tell you whether you should select all the options or just one.

HTH,
Paul.

TwistedLizard
Posts: 57
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.703

Post by TwistedLizard »

From the commentary:
unlike popular belief and unlike mentioned in various books, anonymous class can never be static
If I try and test that, using reflection:

Code: Select all

import java.lang.reflect.*;

public class TestAnonymous{
  static Class myClass1, myClass2;
  public static void main(String[] args){
    myClass1 = (new Runnable(){public void run(){}}).getClass();
    //myClass1 references an anonymous class
    System.out.printf("%b\n", Modifier.isStatic(myClass1.getModifiers()));  //true
    myClass2 = (new String()).getClass();
    //myClass2 references a non-anonymous class
    System.out.printf("%b\n", Modifier.isStatic(myClass2.getModifiers()));  //false
  }
}
output:

Code: Select all

>java TestAnonymous
true
false
Does that not show that an anonymous class can be static?

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

Re: About Question com.enthuware.ets.scjp.v6.2.703

Post by admin »

No, the variables are static, not the class.
Also, the specification says so: https://docs.oracle.com/javase/specs/jl ... jls-15.9.5
An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.1).

TwistedLizard
Posts: 57
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.703

Post by TwistedLizard »

ok thanks, but....

I can't dispute what it says in the jls, which by definition, must be correct, but I'm wondering if in this case reflection doesn't give true answers.
the variables are static, not the class.
In the example I gave, both myClass1 & myClass2 were both declared as static. Yet, the output showed the class referenced by myClass1 to be static and that referenced by myClass2 to be not-static. So, as with all methods, myClass1.getModifiers() must be returning modifiers for the object referenced by myClass1, not the myClass1 variable.

Or, if I eliminate the variables completely and instantiate an anonymous interface implementation

Code: Select all

import java.lang.reflect.*;

public class TestAnonymous2{
  interface ReportMyself{
    void report();
  }
  public static void main(String[] args){
    //invoke report() on an anonymous implementation of ReportMyself 
    (new ReportMyself(){
      public void report(){
        System.out.printf("%s\n", this.getClass());
        System.out.printf("%b\n", Modifier.isStatic(this.getClass().getModifiers()));  //true
      }
     }).report();
  }
}
output

Code: Select all

>java TestAnonymous2
class TestAnonymous2$1
true
the anonymous class is still reported as static.

There must be some basic thing here I'm not getting :)

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

Re: About Question com.enthuware.ets.scjp.v6.2.703

Post by admin »

You are right. I am stumped. To test it further, I added a static variable in the anonymous class like this:

Code: Select all

public class TestClass
{
  interface ReportMyself{
    void report();
  }

  public static class X{
    static int i; //compiles fine.
  }

  public static void main(String[] args){
    //invoke report() on an anonymous implementation of ReportMyself 
    (new ReportMyself(){
     final static int i1 = 0; //compiles fine.
     static int i2 = 0; //doesn't compile.
      public void report(){
        System.out.printf("%s\n", this.getClass());
        System.out.printf("%b\n", Modifier.isStatic(this.getClass().getModifiers()));  //true
      }
     }).report();
  }

}
It proves that there is certainly some differentiation between a static inner class and an anonymous innerclass created in a static method. But it is not clearly why it doesn't say "static" in the modifiers.

I am sorry but I don't have an answer for this :(
Paul.

TwistedLizard
Posts: 57
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.703

Post by TwistedLizard »

ok, thanks for your help Paul.

If even you are stumped, I've probably plumbed this topic to a depth sufficient to answer questions in the exam :)

However, it seems an interesting question, so I've put a post over at javaranch to see if anyone else has comments on the behaviour.

https://coderanch.com/t/681022/java/ref ... -anonymous

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 28 guests