• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing primitives in anonymous inner class

 
Santiago Bravo
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes henry is absolutely right. The statement x = 9; is not allowed inside class outside any method or initializer. change the code to

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags when you post code.
[ November 09, 2008: Message edited by: Jesper Young ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic