Page 1 of 1

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

Posted: Fri Sep 05, 2014 4:44 am
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?

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

Posted: Fri Sep 05, 2014 9:14 am
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.

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

Posted: Sun Jun 11, 2017 5:59 am
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?

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

Posted: Sun Jun 11, 2017 9:22 am
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).

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

Posted: Mon Jun 12, 2017 4:22 pm
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 :)

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

Posted: Mon Jun 12, 2017 9:18 pm
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.

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

Posted: Tue Jun 13, 2017 1:40 am
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