About Question enthuware.ocpjp.v7.2.1383 :
Moderator: admin
-
- Posts: 28
- Joined: Wed Dec 04, 2013 7:57 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1383 :
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,
However #1 is also correct: it will not compile because there are 2 class A
Thanks
Tony,
-
- Site Admin
- Posts: 10228
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
No, the code is fine. You might want to try it out.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 28
- Joined: Wed Dec 04, 2013 7:57 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
It actually works
But why did it allow 2 classes with identical names in the same package?
Thanks,
Tony,
But why did it allow 2 classes with identical names in the same package?
Thanks,
Tony,
-
- Site Admin
- Posts: 10228
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Fri Sep 26, 2014 8:40 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
If option#3 is correct, then how B is extending A ?
Then "This program will not compile." Which implies, option#1 is correct.
Then "This program will not compile." Which implies, option#1 is correct.
-
- Site Admin
- Posts: 10228
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
1. Why do you think option 3 has any relation to B extends A?
2. Did you try compiling the code?
2. Did you try compiling the code?
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Thu Aug 21, 2014 9:34 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
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?
-
- Site Admin
- Posts: 10228
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
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.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?
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 20
- Joined: Fri Jun 19, 2015 9:17 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
The anonymous class created in main() is not final. Please try the following code:
The output is:
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()));
}
Code: Select all
false
-
- Site Admin
- Posts: 10228
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
As per section 15.9.5 of JLS, anonymous class is always implicitly final.
If you like our products and services, please help us by posting your review here.
-
- Posts: 20
- Joined: Fri Jun 19, 2015 9:17 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
Reported a bug to Oracle.
-
- Posts: 62
- Joined: Fri Aug 07, 2015 2:16 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
@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...
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...
-
- Posts: 62
- Joined: Fri Aug 07, 2015 2:16 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
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)
please give me a hint here. ("because inner class B is not static." - doesnt help)
-
- Posts: 62
- Joined: Fri Aug 07, 2015 2:16 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
sorry, and another question:
am I extending the Class TestClass.A anonimously here?:
new TestClass().new A() { public void m() { } };
am I extending the Class TestClass.A anonimously here?:
new TestClass().new A() { public void m() { } };
-
- Site Admin
- Posts: 10228
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
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.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...
If you like our products and services, please help us by posting your review here.
-
- Site Admin
- Posts: 10228
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
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.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)
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Site Admin
- Posts: 10228
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
Yes.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() { } };
If you like our products and services, please help us by posting your review here.
-
- Posts: 62
- Joined: Fri Aug 07, 2015 2:16 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
thanks to the replies, that helped a lot
so anonymously means anonymous type, not "has no variable name"?
so anonymously means anonymous type, not "has no variable name"?
-
- Site Admin
- Posts: 10228
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
Yes, that is correct.sir_Anduin@yahoo.de wrote:thanks to the replies, that helped a lot
so anonymously means anonymous type, not "has no variable name"?
If you like our products and services, please help us by posting your review here.
-
- Posts: 31
- Joined: Tue Oct 06, 2015 1:57 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
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?
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?
-
- Site Admin
- Posts: 10228
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
Yes, you should try it out.
If you like our products and services, please help us by posting your review here.
-
- Posts: 97
- Joined: Wed Dec 28, 2016 9:00 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
Could you post the link please?admin wrote:As per section 15.9.5 of JLS, anonymous class is always implicitly final.
-
- Site Admin
- Posts: 10228
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1383 :
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 6 guests