About Question enthuware.ocpjp.v8.2.1751 :

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

Moderator: admin

Post Reply
jme_chg
Posts: 29
Joined: Sun Feb 07, 2021 6:30 pm
Contact:

About Question enthuware.ocpjp.v8.2.1751 :

Post by jme_chg »

I tested

Make the isFiction method in BookFilter class static and replace LINE 10 with:

Code: Select all

.filter((Book b)->new BookFilter().isFiction(b)) //also works...
this works if the main() is in the Book class...

When I move the main() into a different class in the same package,

Code: Select all

.filter((Book b)->new BookFilter().isFiction(b)) //NOT COMPILE
but I try

Code: Select all

.filter((Book b)->Book.new BookFilter().isFiction(b)) //NOT COMPILE *** even if the main() is in Book
and was expecting this to work, but it does not...

I know that when you have a nested static class, you cannot create it with instance of the outer class
but I don't understand why this wouldn't work?
any explanation for ***?

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

Re: About Question enthuware.ocpjp.v8.2.1751 :

Post by admin »

You should do new Book.BookFilter() instead of Book.new BookFilter(). That is:

.filter((Book b)->new Book.BookFilter().isFiction(b))

This will work from another class.

That's the syntax of creating an instance of static nested class. There is no special reason other than the fact that that is how the grammar of the language is.
If you like our products and services, please help us by posting your review here.

jme_chg
Posts: 29
Joined: Sun Feb 07, 2021 6:30 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1751 :

Post by jme_chg »

Ahhh yes, oops I put Book before the new by accident, cheers.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 33 guests