• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

final

 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator





compiler error is generated at which of the following lines?


1
2
3
4
5

output:
3 and 5

i thought it would be 4 as we are assigning a final value.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi srinivas

i can just explain a bit on this...........as below
since here class Object is anonymous inner class
defined in a method ,
anonymous inner class can access variables of outer
class ie int f1
int f2


and it can access final variabels of method
ie here int p2
int l2

but they cannot access local variables of the method
there are many other rules for anonymous inner class
that you have to read...............


i hope it helps you to some extent..........
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Same code with indentations:

line 1 is ok, local inner class has access to a private static field of the "surrounding" class.

line 2 is also ok, because the method is non-static. It has access to instance fields as well. If the method m1 was static, this would not work.

line 3 does not work, because p1 is not final. Local inner classes have acces to variables of the method itself only when they are final.

line 4 is ok, just the same cause like line 3.

line 5 and 6 the same.

The cause why they have to be final:
See http://www.javaranch.com/campfire/StoryInner.jsp
And look out for red ink.



Yours,
Bu.
[ October 03, 2007: Message edited by: Burkhard Hassel ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic