Page 1 of 1
About Question enthuware.ocpjp.v7.2.1383 :
Posted: Wed Apr 16, 2014 1:19 am
by tn1408
This one could be confusing because #3 and #4 are correct.
However #1 is also correct: it will not compile because there are 2 class A
Thanks
Tony,
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Wed Apr 16, 2014 2:10 am
by admin
No, the code is fine. You might want to try it out.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Wed Apr 16, 2014 5:39 pm
by tn1408
It actually works
But why did it allow 2 classes with identical names in the same package?
Thanks,
Tony,
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Wed Apr 16, 2014 10:02 pm
by admin
Because the scope of the two classes is different. One belongs to a package, and the other one belongs to a class inside that package.
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Fri Oct 31, 2014 6:43 am
by vijayanand
If option#3 is correct, then how B is extending A ?
Then "This program will not compile." Which implies, option#1 is correct.
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Fri Oct 31, 2014 6:51 am
by admin
1. Why do you think option 3 has any relation to B extends A?
2. Did you try compiling the code?
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Fri Nov 21, 2014 7:50 am
by Veritas
there are some questions (like this one) where 1 option doesn't make any sense. how would it be possible to select "none of these" if you are supposed to select 2 options?
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Fri Nov 21, 2014 8:39 am
by admin
Veritas wrote:there are some questions (like this one) where 1 option doesn't make any sense. how would it be possible to select "none of these" if you are supposed to select 2 options?
You are right. Unfortunately, it is the side effect of having the option to hide number of correct options. When a user chooses not to display the number of correct options with each question, it makes sense.
-Paul.
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Fri Jun 26, 2015 7:29 am
by Alexey Berezkin
The anonymous class created in main() is not final. Please try the following code:
Code: Select all
public static void main(String args[]) {
A a = new TestClass().new A() {
public void m() {
}
};
System.out.println(Modifier.isFinal(a.getClass().getModifiers()));
}
The output is:
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Fri Jun 26, 2015 10:17 am
by admin
As per section 15.9.5 of JLS, anonymous class is always implicitly final.
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Fri Jun 26, 2015 10:45 am
by Alexey Berezkin
Reported a bug to Oracle.
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Tue Nov 24, 2015 2:55 pm
by sir_Anduin@yahoo.de
@Alexey Berezkin
as i understand you are testing for a of type A. in this case a is the name, and thereby not annonymous.
You should rather (if possible) check new TestClass().new A()..... this one should return true...
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Tue Nov 24, 2015 3:03 pm
by sir_Anduin@yahoo.de
But I stil dont understand why you cant say new A() or new B()...
please give me a hint here. ("because inner class B is not static." - doesnt help)
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Tue Nov 24, 2015 3:05 pm
by sir_Anduin@yahoo.de
sorry, and another question:
am I extending the Class TestClass.A anonimously here?:
new TestClass().new A() { public void m() { } };
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Tue Nov 24, 2015 8:27 pm
by admin
sir_Anduin@yahoo.de wrote:@Alexey Berezkin
as i understand you are testing for a of type A. in this case a is the name, and thereby not annonymous.
You should rather (if possible) check new TestClass().new A()..... this one should return true...
No, his code does a.getClass(), where a is pointing to an instance of the anonymous class. a is a variable name. It is not the name of the anonymous class.
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Tue Nov 24, 2015 8:32 pm
by admin
sir_Anduin@yahoo.de wrote:But I stil dont understand why you cant say new A() or new B()...
please give me a hint here. ("because inner class B is not static." - doesnt help)
Since class B is not a static class, it can only be created within the context of an instance of TestClass. In other words, every instance of B, has to have a reference to an instance of its outer class TestClass. If you do new A() in main, the instance of A will not be able to use "this" reference to refer to an instance of TestClass because main is static. That is why you need to do new TestClass().new A(). Java language provides this way to associate an instance of the outer class with the inner class.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Tue Nov 24, 2015 8:32 pm
by admin
sir_Anduin@yahoo.de wrote:sorry, and another question:
am I extending the Class TestClass.A anonimously here?:
new TestClass().new A() { public void m() { } };
Yes.
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Wed Nov 25, 2015 3:20 pm
by sir_Anduin@yahoo.de
thanks to the replies, that helped a lot
so anonymously means anonymous type, not "has no variable name"?
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Wed Nov 25, 2015 6:57 pm
by admin
sir_Anduin@yahoo.de wrote:thanks to the replies, that helped a lot
so anonymously means anonymous type, not "has no variable name"?
Yes, that is correct.
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Sat Jan 30, 2016 6:42 pm
by krohani
So I just want to verify something based on confusion from this question versus another question.
Inside of the main method we do not implicitly have an instance of this and therefore we need to do a new TestClass().new B().....
however if we were inside of a method inside TestClass say public void method() { } then inside of that method we could simply do a new B() without needing an explicity instantiation of TestClass with it, correct?
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Sat Jan 30, 2016 9:21 pm
by admin
Yes, you should try it out.
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Mon Jan 02, 2017 11:16 am
by jagoneye
admin wrote:As per section 15.9.5 of JLS, anonymous class is always implicitly final.
Could you post the link please?
Re: About Question enthuware.ocpjp.v7.2.1383 :
Posted: Mon Jan 02, 2017 12:14 pm
by admin