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

appliedreasoning #60

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from appliedreasoning # 60
Which of the following code fragments will not cause a compile/runtime error at the <code here> section?
public class InnerTest
{
private String s = "outer";
private static int time = 1;

public static void main(String[] args)
{
new InnerTest().run(args);
}
public void run(String[] args)
{
int num = 4;
final int finalNum = 2;
class Inside
{
public void execute()
{
<code here>
}
}
new Inside().execute();
}
}
A. System.out.println(s);
B. System.out.println(args);
C. System.out.println(num);
D. System.out.println(time);
E. System.out.println(finalNum);
I picked E because I very have very specifically spelled out in my notes that a Local Nested Inner Class can access any final local variables in their scope. The solution claims that A, D, E are correct but I fail to see how s or time are final local variables in the scope of this Local Inner Class. I compiled the various solutions they suggest and indeed A, D & E all work fine. Am I missing something or do I have a misconception as to what final local variables are?. Please help.
Thanks in advance for the input ... Gerry

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are fine on that concept. a local inner class can only access local variables if they are final. however s and time are member variables of the enclosing class(not local variables). the local inner class has access to them.
 
Well THAT's new! Comfort me, reliable tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic