Page 1 of 1

About Question enthuware.ocpjp.v8.2.1751 :

Posted: Mon Apr 12, 2021 6:34 am
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 ***?

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

Posted: Mon Apr 12, 2021 7:06 am
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.

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

Posted: Mon Apr 12, 2021 7:37 am
by jme_chg
Ahhh yes, oops I put Book before the new by accident, cheers.