The code compiles and runs, but does not print "foofy". Based on previous discussions, I have tried several things:
MyWonderfulClass.java:12: incompatible types
found : void
MyWonderfulClass my = new MyWonderfulClass().go();
MyWonderfulClass.java:13: cannot find symbol
symbol : method doStuff()
location: class MyWonderfulClass
Then I tried:
MyWonderfulClass.java:13: void cannot be dereferenced
What can I change in the main() method such that the code will print foofy? Inner classes seems more challenging to grasp.
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
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
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
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%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Joined: Feb 18, 2010
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...
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