This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
hi, All the member functions, varaibles, classes declared with static keyword, can be accessed directly by specifying the name of the class itself & need not be accessed thro' instance of the class. i guess this is the reason,
with regds anitha
Joined: Aug 28, 2003
Hi Anitha, yes I know that they can be accessed via their class name (myOuter) or by one of their instances (outer). The question is why in that case (myOuter=non-static, myInner=static) this does not work, and i have to use the class, while the reference via an instance causes a compiler error. I mean why does the compiler tell me that
is a problem? As he should "resolve" this to which works fine. Regards Dirk
I don't claim to completely understand the ins and outs of inner classes, and I hope what I post won't add to the confusion. Look at this code snippet:
Quoting from "Java in a Nutshell" regarding static member classes,
In code outside of the containing class, a static member class or interface is named by combining the name of the outer class with the name of the inner class.
So "myOuter.myInner.myConst" is well-formed and well-defined, but you should think of it as two components ["myOuter.myInner" as the class name/path, "myConst" as the static instance variable reference] as opposed to three components ["myOuter", "myInner" and "myConst"]. Within the "myOuter" class you can reference "myInner" w/out any qualifications, but outside of the "myOuter" class--as, for example, within the "myTest" class--the only way to refer to the inner class is to use "myOuter.myInner". So simply using "outer.myInner" has no meaning. So in the code I posted I added: myOuter.myInner inner = new myOuter.myInner(); which shows that you can refer to the "myInner" class, but you have to fully qualify it. I hope this helps a bit ...
Joined: Aug 28, 2003
Hi Wayne, thanks for you post and in fact this make sense... Thanks and regards Dirk