jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous class problem 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 "Anonymous class problem" Watch "Anonymous class problem" New topic
Author

Anonymous class problem

Prasun Howlader
Ranch Hand

Joined: Oct 21, 2007
Posts: 89
public class Prg{
int i = 12;
public static void main(String[]args){
final int i = 14;
fun(new Prg(){{System.out.println(i);}});
}
public static void fun(Prg ob){}
}
When i run this program it despaly 12 .
1)Why i values in the Prg class dispaly here?
2)How can access local i in the method main?


"Control time instead of letting time control you."
Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
Hi,Prasun Howlader
I think the code"System.out.println(i);" should be in a method.
Anonymous class is like the normal class except it`s location.


who dare win!<br />SCJP5(94%)|SCWCD5(86%)|SCBCD(100%)|SCEA in progress
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Jia Fudong:
...I think the code"System.out.println(i);" should be in a method...

It's executing as an initialization block.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Prasun Howlader:
... When i run this program it despaly 12 .
1)Why i values in the Prg class dispaly here?
2)How can access local i in the method main?

When you define the anonymous class body, note that you are subclassing Prg, so the anonymous instance has an inherited variable 'i' with a value of 12.

I don't think it's possible to reference main's local variable 'i' from the context of this anonymous class body.
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Prasun Howlader:
1)Why i values in the Prg class dispaly here?
2)How can access local i in the method main?[/QB]


Prasun, thanks for posting a pretty interesting question! As Marc pointed out, the anonymous subclass's i instance variable shadows the local variable in main(), so there's no way to access it directly from the method body.

However, do note one subtle point: the instance variable i is visible within the anonymous subclass because the anonymous subclass is created in a method (main()) within the same package as Prg. However, because you declared i with package-private (i.e. default) visibility, it will not be visible outside the package. In other words:
will output "14", whereas
will output "12" (just like your original code).

Note also that if your original code declared Prg's i instance variable as private, your program will also output "14".
[ November 05, 2007: Message edited by: Kelvin Lim ]

SCJP 5.0
 
 
subject: Anonymous class problem
 
Similar Threads
Static int
Method Overloading problem
Variable length method
Static Int and Methods
static block