This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How will it execute? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How will it execute?" Watch "How will it execute?" New topic

How will it execute?

Shailesh Phatak
Ranch Hand

Joined: Aug 27, 2009
Posts: 64
How do i execute this type of inner class?

How do i call this so that the ans would be "Human drinks water in main class"

See Dreams With Open Eyes And Make It Real
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

There are a few syntex errors in your code. Can you please refine and post it again?
Shailesh Phatak
Ranch Hand

Joined: Aug 27, 2009
Posts: 64
I cant.. I tried a lot to execute it anyhow. But i am sure that there is no syntax error in go() part of InnerAnoInterface Class
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

Try this...

SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Shailesh Phatak
Ranch Hand

Joined: Aug 27, 2009
Posts: 64
Thanks MM for your help.. But what kind of magic going on here ?
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
There are several things to look at. First, on line 06 below, go() is called
on the anonymous class created by new. Okay so far?
Then looking at the definition of Water, its drinkWater() method immediately calls
drink() on an Interf object that is passed in. The key to the puzzle is to figure out
how this Interf object is created.

Notice below that the parameter passed to drinkWater() continues all the way from
the 'new' on line 02 through line 06. The drinkWater() call ends with ); on line 06.Below again is the type Interf parameter from above. Line 07 starts a rewritten version
that illustrates what is happening.So you can 'new' an interface, creating an anonymous class, if you implement the interface's
methods in this way. Right in the call parameter list, Interf is implemented by a 'no-name'
class and then the 'no-name' class is instantiated as a type Interf object. Pretty cool, ey?

Jim... ...

Shailesh Phatak
Ranch Hand

Joined: Aug 27, 2009
Posts: 64
Thanks Jim for the elaborative reply
I used the correct word i think in the last post i.e "MAGIC"
I agree. Here's the link:
subject: How will it execute?
It's not a secret anymore!