My qns. is can an inner class defined within a method access the static or private member fields(variables) of the enclosing(outer) class? If so how? Thank You
Yes, an inner class can access private member fields of the enclosing outer class. An inner class can access static member fields of the enclosing outer class, if the inner class is also static. If so how? I am working on the code...will post it soon....
Output: Outer 5 Inner 55 Outer 7 Sorry for the delay. Regds. - satya
[This message has been edited by satya5 (edited June 04, 2000).]
Herbert Maosa
Ranch Hand
Joined: May 03, 2000
Posts: 289
posted
0
Satya, I think Basu is asking about local inner classes.
Basu, Indeed an inner class declared inside a method ( a local inner class) may acess the ALL variables of the enclosing outer class. These variables are member variables, and by virtue of this fact, they are available globally throughout the class.This means that the way to access them is simply to use their names as they are originally declared.For instance variables you can just use the name, and for static variables you may use either the instance name or the class name prepended to the variable.Bear in mind that if your local inner class has variables that have the same names as those in the outer class, then the outer class variables will not be visible just by the name. Herbert.
Basu Patel
Ranch Hand
Joined: May 28, 2000
Posts: 60
posted
0
Thank You Satya5 and Herbert M. for your time ! :-)
Ajith Kallambella
Sheriff
Joined: Mar 17, 2000
Posts: 5782
posted
0
Herbert, I will take your statement with a pinch of salt.
Indeed an inner class declared inside a method ( a local inner class) may acess the ALL variables of the enclosing outer class.
Actually it is not ALL variables in the enclosing context, but only what is visible to the enclosing context. Consider a local inner class within a static method. The inner class cannot access the instance variables of the class because there is no 'this' reference. Consider the following code to see what I mean <PRE> class StaticInnerAccessDemo { int instanceVar = 10 ;
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
So...am I right in saying "static inner (local or not) cannot access non-static member variables." (since the this is not available). I am trying to locate such a stmt in RHE right now.... Herbert: I don't mind you correcting, but I think it was your assumption that Basu was asking for local inner classes....I don't find the word "local" in Basu's post.... Thanks. - satya
Ajith Kallambella
Sheriff
Joined: Mar 17, 2000
Posts: 5782
posted
0
Satya, Yes. You are right. Rule of thumb is, see what the enclosing context can access. The enclosed context can access all that, and just that. The accessibility restriction can come from other things too, so let us not think only about static/non-static combination. Ajith
Suma Narayan
Ranch Hand
Joined: Apr 03, 2000
Posts: 136
posted
0
Satya,
So...am I right in saying "static inner (local or not) cannot access non-static member variables." (since the this is not available).
A small correction: As far as I know, a local inner class 1. Cannot be static 2. Must not have any access modifier. So, there is no question of static local inner class. But, as Ajith says, a local inner class can access only what is visible to the enclosing context. A local inner class within a static method cannot access the instance variables of the class because there is no 'this' reference. Suma.
Ajith Kallambella
Sheriff
Joined: Mar 17, 2000
Posts: 5782
posted
0
OOps!! I won't say the $1 ( five letter ) magic word. May be I didn't read the ( static or not ) part of Satya's post. Thanks Suma. Ajith [This message has been edited by Ajith Kallambella (edited June 05, 2000).]
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
So summarizing all this:
local class has access to all variables of the enclosing method(context) which are eligible (alias final method variables and outer class variables)
local class in a static method cannot access outer class variables, since there is no this reference. So only static variables of outer class and final method variables.
static inner class (not local) cannot access non-static outer-class-variables (instance variables) since there is no this (outer) reference.
do i offer the truth and nothing but the truth ? My confusion is about the third point in the list. I seriously need to write some code now ...... Regds. - satya thxs Sandra....i stand corrected... knock knock ..... how abt the third point ? thxs Ajith....i stand corrected... anything else....Ajith, i also added the word non-static to be more accurate. FYI... [This message has been edited by satya5 (edited June 05, 2000).] [This message has been edited by satya5 (edited June 05, 2000).] [This message has been edited by satya5 (edited June 05, 2000).] [This message has been edited by satya5 (edited June 05, 2000).]
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Satya, In your Summary, the second point should say,'local class in a static method cannot access outer class instance variables,since there is no this reference. So only static class variables and final method variables.
Ajith Kallambella
Sheriff
Joined: Mar 17, 2000
Posts: 5782
posted
0
Satya, Your third point should say static inner class (not local) cannot access outer class instance variables since there is no this(outer) reference. I know I am being very picky Ajith
Deepa sivasankar
Ranch Hand
Joined: Jul 22, 2003
Posts: 35
posted
0
One small doubt. We can create an instance of the outer class from the inner class right. From Sandra reply , quoted below. "In your Summary, the second point should say,'local class in a static method cannot access outer class instance variables,since there is no this reference. So only static class variables and final method variables." We can access the instance variables of enclosing class through its instance. Look at the code below. public class Outer { private int x = 5; static int y = 6; public static void main(String args[]) { System.out.println("in main"); Outer.method(); System.out.println("end of main"); } static void method() { class inner { inner() { System.out.println(new Outer().x); } } new inner(); } } Produces the output in main 5 end of main Any comments. Thanks Deepa
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Deepa, You are right. Forget about inner classes for the moment. Any static method can use the class instance variables only in the context of an object of that class. Inner classes can in turn access all the variables of the outer class which its enclosing method can access. If you have any doubts refer to satya's points above.
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Deepa: The difference between your code and my code is the foll.: Your code: The Outer class object (thru which you access the instance variable x) is created in the Inner class. An instance can always access its variables right! Hence the print stmt works fine. My Code (all the way at the top): The Outer class object (thru which I access the instance variable x is defined in the main method (object refered by ot1). Please see that the print stmt is commented out // System.out.println("Outer " + Outer.this.x); in the Inner class. This is because I donot have the this reference in the static Inner class. Hope this helps. You agree ? Regds. - satya
Deepa sivasankar
Ranch Hand
Joined: Jul 22, 2003
Posts: 35
posted
0
Satya, I agree with you. // System.out.println("Outer " + Outer.this.x); The above syntax is useful if you have the same variable x within the inner class and you want to use the variable in the Outer class. Thanks
Herbert Maosa
Ranch Hand
Joined: May 03, 2000
Posts: 289
posted
0
Oops, a lot has been discussed and I was away and missed the fun.I think the fun continues.... Satya you wrote : ---------------------------------------------------- Herbert: I don't mind you correcting, but I think it was your assumption that Basu was asking for local inner classes....I don't find the word "local" in Basu's post.... ------------------------------------------------------------- But Satya, read again Basu's question. From my terminal it reads .....can an inner class DEFINED WITHIN A METHOD......... So I thought the capitalized part is what defines an inner class LOCAL.? Ajith, of course you are right and I hope Basu has taken note. By All member variables of the enclosing outer class I really meant those that are visible to the method in which the inner class is defined. Herbert
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
daaaaaaaa...... .....must have been the lack of proper coffee nutrition....maybe I should do what the guys in the Geico Ad do.... thxs for pointing out....i hope I read the qstn correctly in the exam. Have fun. Regds. - satya
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
What is the static local class does not exsits in java ? what does it mean. Give a explaination for static local class?
Indu mathy
Greenhorn
Joined: Jun 08, 2000
Posts: 8
posted
0
can we place a static class inside a static method
Indu
Herbert Maosa
Ranch Hand
Joined: May 03, 2000
Posts: 289
posted
0
Indu, NO!, you can not place a static class inside a method. Static local inner classes do not exist. Herbert.
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Something like this:
Infact you cannot even declare a static variable in a method (static or not). This is another important point. Hope this helps. Regds. - satya
Greg Whelan
Ranch Hand
Joined: May 18, 2000
Posts: 52
posted
0
static local inner classes do exist -- they are never explicitly labelled as "static". A local inner class defined within a static method (or a static initializer block) is implicitly static The question I have is: since javac doesn't allow you to define a static member variable in a static local class (which is something that IBM's jikes *does* allow), what is the use of a static local inner class? (btw, I'm going to guess that this is beyond the scope of SCJP) The following code demonstrates how a semi-useful static inner class (which won't compile with "static int x" under javac, remove the "static"):
That will print 0, 1, 2, 3 (again, compiled with Jikes). Can anyone think of a static local class that does something useful?
Greg Whelan
Ranch Hand
Joined: May 18, 2000
Posts: 52
posted
0
Just for the record, my source on the validity of static local classes is Mughal & Rasmussen's "A Programmers Guide to Java Certification" (discussed in section 7.4). They say the following (there legalistic style has obviously pervaded my own thinking ):
A local class cannot be specified with the keyword static. However, if the context is static (ie. a static method or a static initializer) then the local class is implicitly static. Otherwise the local class is non-static.
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
I would also like to mention one more point 'static nested class can have static members, whereas a non-static nested class cannot'