About Question com.enthuware.ets.scjp.v6.2.703
Moderator: admin
-
- Posts: 4
- Joined: Sat Aug 30, 2014 4:19 am
- Contact:
About Question com.enthuware.ets.scjp.v6.2.703
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?
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?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.703
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.
HTH,
Paul.
-
- Posts: 57
- Joined: Sat Mar 01, 2014 1:48 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.703
From the commentary:
output:
Does that not show that an anonymous class can be static?
If I try and test that, using reflection:unlike popular belief and unlike mentioned in various books, anonymous class can never be static
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
}
}
Code: Select all
>java TestAnonymous
true
false
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.703
No, the variables are static, not the class.
Also, the specification says so: https://docs.oracle.com/javase/specs/jl ... jls-15.9.5
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).
-
- Posts: 57
- Joined: Sat Mar 01, 2014 1:48 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.703
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.
Or, if I eliminate the variables completely and instantiate an anonymous interface implementation
output
the anonymous class is still reported as static.
There must be some basic thing here I'm not getting
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.
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.the variables are static, not the class.
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();
}
}
Code: Select all
>java TestAnonymous2
class TestAnonymous2$1
true
There must be some basic thing here I'm not getting

-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.703
You are right. I am stumped. To test it further, I added a static variable in the anonymous class like this:
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.
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();
}
}
I am sorry but I don't have an answer for this

Paul.
-
- Posts: 57
- Joined: Sat Mar 01, 2014 1:48 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.703
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
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
Who is online
Users browsing this forum: Google [Bot] and 28 guests