• 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

Inner class

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any one can you solve this problem and explain how?

In the following code for a class in which doSomething has an inner class, which variables would the statement in line 13 be able to use in place of XX?

public class Qa2

{
private static final int ID = 3;

private String oha;

public void doSomething( final int yy )

{

int number = 3;

class Palmovka

{

float fifi = 28.45F;

void doSomethingMore()

{

System.out.println(XX);

}

}
}

new Palmovka().doSomethingMore();

}

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


Please always put your code in "code" tag. It improves readability.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rex
System.out.println(XX) here can use the int ID variable only since it is final.

Why ?
Because method-local innner classes can only access final local varibles of method in which they are defined .
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following are the legal variables that you can use:

System.out.println(yy); -- final local variable can be used in a method local class.

System.out.println(fifi); -- its the instance variables for the class

System.out.println(ID); -- all members(even private) of the outer class are accessible to the innerclass

System.out.println(oha); -- all members(even private) of the outer class are accessible to the innerclass

System.out.println(number); this is illegal as variables local to a method cannot be accessed by the inner class, to access we have to make them final.

Hope this helps.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic