About Question enthuware.ocpjp.v7.2.1383 :

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

Moderator: admin

Post Reply
tn1408
Posts: 28
Joined: Wed Dec 04, 2013 7:57 pm
Contact:

About Question enthuware.ocpjp.v7.2.1383 :

Post 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,

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

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post by admin »

No, the code is fine. You might want to try it out.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

tn1408
Posts: 28
Joined: Wed Dec 04, 2013 7:57 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post by tn1408 »

It actually works
But why did it allow 2 classes with identical names in the same package?

Thanks,

Tony,

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

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post 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.
If you like our products and services, please help us by posting your review here.

vijayanand
Posts: 10
Joined: Fri Sep 26, 2014 8:40 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post 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.

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

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post by admin »

1. Why do you think option 3 has any relation to B extends A?
2. Did you try compiling the code?
If you like our products and services, please help us by posting your review here.

Veritas
Posts: 12
Joined: Thu Aug 21, 2014 9:34 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post 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? :)

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

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post 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.
If you like our products and services, please help us by posting your review here.

Alexey Berezkin
Posts: 20
Joined: Fri Jun 19, 2015 9:17 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post 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:

Code: Select all

false

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

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post by admin »

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.

Alexey Berezkin
Posts: 20
Joined: Fri Jun 19, 2015 9:17 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post by Alexey Berezkin »

Reported a bug to Oracle.

sir_Anduin@yahoo.de
Posts: 62
Joined: Fri Aug 07, 2015 2:16 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post 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...

sir_Anduin@yahoo.de
Posts: 62
Joined: Fri Aug 07, 2015 2:16 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post 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)

sir_Anduin@yahoo.de
Posts: 62
Joined: Fri Aug 07, 2015 2:16 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post 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() { } };

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

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post 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.
If you like our products and services, please help us by posting your review here.

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

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post 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.
If you like our products and services, please help us by posting your review here.

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

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post 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.
If you like our products and services, please help us by posting your review here.

sir_Anduin@yahoo.de
Posts: 62
Joined: Fri Aug 07, 2015 2:16 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post by sir_Anduin@yahoo.de »

thanks to the replies, that helped a lot
so anonymously means anonymous type, not "has no variable name"?

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

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post 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.
If you like our products and services, please help us by posting your review here.

krohani
Posts: 31
Joined: Tue Oct 06, 2015 1:57 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post 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?

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

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post by admin »

Yes, you should try it out.
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post by jagoneye »

admin wrote:As per section 15.9.5 of JLS, anonymous class is always implicitly final.
Could you post the link please? :)

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

Re: About Question enthuware.ocpjp.v7.2.1383 :

Post by admin »

If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 37 guests