wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous Inner Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonymous Inner Class " Watch "Anonymous Inner Class " New topic

Anonymous Inner Class

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434

Sierra/Bates, Chapter 8:

The code compiles and runs, but does not print "foofy". Based on previous discussions, I have tried several things:

Compiler outputs:

MyWonderfulClass.java:12: incompatible types
found : void
required: MyWonderfulClass
MyWonderfulClass my = new MyWonderfulClass().go();
MyWonderfulClass.java:13: cannot find symbol
symbol : method doStuff()
location: class MyWonderfulClass
2 errors

Then I tried:

Compiler outputs:

MyWonderfulClass.java:13: void cannot be dereferenced
1 error

What can I change in the main() method such that the code will print foofy? Inner classes seems more challenging to grasp.

Marriage Made in Heaven
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

when we are creating the anonymous class at the method argument
we are directly creating the object of the class
when we are declaring the method

then what do we do here?
it states that the method argument variable f of type Foo is referring to the object that is passed to this method
now here
when we actually pass the argument to the method

we know that the method foof() is of the class (actually anonymous class here ) that implements the interface Foo
we know that the method foof() can be called only by using the reference of type class that implements Foo interface
according to our program only the reference variable f in the method declaration doStuff(Foo f) can refer to such class the implements Foo interface
polymorphism applies here
we are referring to the subclass (in this case the implementing class) object using the super type reference (here the reference is interface type)'
to call the method foof() we can use only the variable f
and also the variable f will be available only for the method doStuff() as that is the method argument
so we call the foof() method in doStuff() method as follows

this should solve the problem

Now let us talk about the things you have done wrong in your program

the code means
1. Create a variable my of type MyWonderfulClass
2. Create a new object of type MyWonderfulClass and call the go method of that class
3. notice here that the go() method returns nothing i.e. the return type of the method is void
4. and using the assignment operator you are trying to assign a void type to MyWonderfulClass type (notice here that null and void are totally different)

this causes the error incompatible types
and also
doStuff() method belongs to the class Bar and you are calling it using the my variable hence the error cannot find symbol as the MyWonderfulClass does not contain doStuff( )method

the code

cannot compile because here again you made the same mistake
go() returns void and we cannot call doStuff() method using the void (void cannot be deferenced)

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Hi Prasad,

Thank you for the explanation. I had to re-read it several times, and I somewhat grasp it (I think inner classes are at the same complexity as Regular Expressions ).

I re-wrote the code, which compiles and runs:

I just don't understand why the Sierra/Bates book didn't include f.foof() in the method doStuff() in class Bar, i.e.

Is there any other way this code would work without including the f.foof()? That is what I'm trying to experiment with right now...

Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

well the variable f is available only in the method as it acts as the local variable (but it is not local variable, it is argument)
so we cannot access the variable outside the method but still if we want to
then we have to do some ridiculous things
try following code
change the main method to

and the Bar class to

Hope this helps
Happy preparation
I agree. Here's the link: http://aspose.com/file-tools
subject: Anonymous Inner Class
Similar Threads
Argument-Defined Anonymous Inner Class
Argument defined Annonymous Inner class
Anonymous inner class
Argument-defined anonymous class call
Anonymous class