• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question abt scope of variables frm Mock Test

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
one question which I got frm danchisholm Mock Test which says...

class A {
private static int f1 = 1;
private int f2 = 2;
void m1(int p1, final int p2) {
int l1 = 5;
final int l2 = 6;
Object x = new Object() {
int a = f1; // 1
int b = f2; // 2
int c = p1; // 3
int d = p2; // 4
int e = l1; // 5
int f = l2; // 6
};
}
}

Compiler errors are generated at which of the following lines?
a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. None of the Above.
ans given is
c.)3 & e.)5
I could n't get the explanation for this. can anybody help me out?
:roll:
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From an inner class, you can only access local variables or parameters if they are declared final. From the JLS, §8.1.2 Inner Classes and Enclosing Instances:


Any local variable, formal method parameter or exception handler parameter used but not declared in an inner class must be declared final, and must be definitely assigned (�16) before the body of the inner class.


Hope that helps,
Corey
reply
    Bookmark Topic Watch Topic
  • New Topic