| Author |
Accessing primitives in anonymous inner class
|
Santiago Bravo
Ranch Hand
Joined: Jul 25, 2008
Posts: 226
|
|
Hi All, Code example is from K&B but which shows how to create an anonymous inner class. BUT I am curious as to how a primitive can be accessed in the anonymous inner class: **************************************** class Popcorn { public int x = 2; // Line 1 public void pop() { System.out.println("popcorn"); } } class Food { Popcorn p = new Popcorn() { // Line 2 x=9; // Line 3 - compiler error! public void pop() { System.out.println("anonymous popcorn"); } }; } **************************************** Line 2 creates the anonymous subclass which according to polymorphism should have access to public members of the superclass. Why doesnt the above code compile? What am I doing wrong? thanks
|
Santiago
My Path to SCJP Certification My Path to SCWCD Certification
|
 |
Rekha Srinath
Ranch Hand
Joined: Sep 13, 2008
Posts: 178
|
|
The x=9 is in your anonymous inner class is like an instance variable, and in general, for an instance variable, you should mention the type. Take a normal example as this: Even in this code, you will get the same compiler error. So, you have to DECLARE the variable as an instance variable. But, you can access the inherited variable in any of the child methods in the normal way. For eg, in your Popcorn example, you can have like this: System.out.println("anonymous popcorn" + x); // x will be 2 here, inherited from the parent Popcorn
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32693
|
|
But putting fields in a subclass which have the same name as a superclass field will "hide" the superclass field. This can lead to all sorts of confusion later on.
|
 |
Santiago Bravo
Ranch Hand
Joined: Jul 25, 2008
Posts: 226
|
|
yes, if I had declared int x = 9; on line 3 then this is a totally new variable for the instance. I can access variable x from the Popcorn class if I use it in a method in the anonymous inner class
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Originally posted by Santiago Bravo: yes, if I had declared int x = 9; on line 3 then this is a totally new variable for the instance. I can access variable x from the Popcorn class if I use it in a method in the anonymous inner class
Not sure what you are asking. Are you trying to assign the variable, without declaring a new one? If you are, then it is a statement, not a declaration. And statements are not allowed outside of an initializer, constructor, or method. Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
yes henry is absolutely right. The statement x = 9; is not allowed inside class outside any method or initializer. change the code to
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12928
|
|
Please use code tags when you post code. [ November 09, 2008: Message edited by: Jesper Young ]
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: Accessing primitives in anonymous inner class
|
|
|