• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Accessing primitives in anonymous inner class

 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 75631
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags when you post code.
[ November 09, 2008: Message edited by: Jesper Young ]
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic