• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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 ]
 
Whip out those weird instruments of science and probe away! I think it's a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic