This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes I have some questions regarding the access in enclosing context of static and non-sta Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I have some questions regarding the access in enclosing context of static and non-sta" Watch "I have some questions regarding the access in enclosing context of static and non-sta" New topic
Author

I have some questions regarding the access in enclosing context of static and non-sta

sura watthana
Ranch Hand

Joined: Sep 13, 2004
Posts: 77
I have some questions regarding the access in enclosing context of static and non-static local class.
looking at the code below
what i don't understand why line //(7) and //(8) are allowed but not for line //(17) and //(18)

could somebody please help me?

thank you

code:

Example 7.9 Access in Enclosing Context
class Base {
protected int nsf1;
}
class TLCWithLocalClasses { // Top level Class
private double nsf1; // Non-static field
private int nsf2; // Non-static field
private static int sf; // Static field

void nonStaticMethod(final int fp) { // Non-static Method
final int flv = 10; // final local variable
final int hlv = 30; // final (hidden) local variable
int nflv = 20; // non-final local variable

class NonStaticLocal extends Base { // Non-static local class
// static int f1; // (1) Not OK. Static members not allowed.
final static int f2 = 10; // (2) final static members allowed.
int f3 = fp; // (3) final param from enclosing method.
int f4 = flv; // (4) final local var from enclosing method.
// double f5 = nflv; // (5) Not OK. Only finals from enclosing method.
double f6 = nsf1; // (6) Inherited from superclass.
double f6a = this.nsf1; // (6a) Inherited from superclass.
double f6b = super.nsf1; // (6b) Inherited from superclass.
double f7 = TLCWithLocalClasses.this.nsf1;// (7) In enclosing object.
int f8 = nsf2; // (8) In enclosing object.
int f9 = sf; // (9) static from enclosing class.
int hlv; // (10) Hides local variable.
}
}

static void staticMethod(final int fp) { // Static Method
final int flv = 10; // final local variable
final int hlv = 30; // final (hidden) local variable
int nflv = 20; // non-final local variable

class StaticLocal extends Base { // Static local class
// static int f1; // (11) Not OK. Static members not allowed.
final static int f2 = 10; // (12) final static members allowed.
int f3 = fp; // (13) final param from enclosing method.
int f4 = flv; // (14) final local var from enclosing method.
// double f5 = nflv; // (15) Not OK. Only finals from enclosing method.
double f6 = nsf1; // (16) Inherited from superclass.
double f6a = this.nsf1; // (16a) Inherited from superclass.
double f6b = super.nsf1; // (16a) Inherited from superclass.
// double f7 = TLCWithLocalClasses.this.nsf1;//(17) No enclosing object.
// int f8 = nsf2; // (18) No enclosing object.
int f9 = sf; // (19) static from enclosing class.
int hlv; // (20) Hides local variable.
}
}
}
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
It would be helpful if you trim this example down to the cases which you do not understand. Also use [C0DE][/C0DE] tags around your code to preserve indentation.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Mahesh Bhatt
Ranch Hand

Joined: Sep 15, 2004
Posts: 88
hey ... good question there ...even i was confused for a while coz the code is kind of complex and lengthy ..... u see ... in line //7 and //8 ....the only thing u gotta look at ..is ... the local class declared just before //1. is inside a non static code/method ...so u can use "this" keyword there ....however ...at //17 and //18 .. the class is declared in a static context and thus doesnt belong to any object ....so u cant use the "this" keyword there ... .it would be really good if u run this in ur system ...it would give u a compilation errror stating the use of this ...i guess ... so bottomline ... this cant be used in a static context ....there the problem lies ..... gottit .... if no ...plz mail me ...willl send a detailed answer ..... try a few more examples also ...untill u get very comfetable with these inner classes .....i would also recommend u go through the campfire stories in this site ... there is one story that deals with inner classes ....really cleared my doubts


Impossible is I M Possible
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: I have some questions regarding the access in enclosing context of static and non-sta
 
Similar Threads
Inner Local Classes
question regarding the permission to access non-final local variables.
Local Class in a Static Method
Please rectify this code on anonymous classes
static method local classes