aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Accessing primitives in 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 "Accessing primitives in anonymous inner class" Watch "Accessing primitives in anonymous inner class" New topic
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: 38765
    
  23
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: 18825
    
  40

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
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14144
    
  18

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing primitives in anonymous inner class