Author | Topic: Static inner class |
Jimnap unregistered |
posted March 09, 2000 11:03 AM
How can I access the static variable of a static inner class from an instance of the enclosing outer class ?
|
maha anna bartender |
posted March 09, 2000 11:19 AM
Like this...
regds maha anna.
|
Deep Java unregistered |
posted March 09, 2000 12:25 PM
I have to disagree with Maha Ana here.... * No need of instance of outerclass for static innerclass. This works just fine.... class Test {
|
maha anna bartender |
posted March 09, 2000 01:15 PM
Deepa, your way is also correct. But see the qstn. It asks specifically from an instance of an outer class. So I went for this method. How can I access the static variable of a static inner class from an instance of the enclosing outer class By the way, maha's (I usually don't like to say mine/my/me etc..) method also works. regds maha anna
|
Jim Yingst sheriff |
posted March 10, 2000 12:19 AM
[nitpicking alert - none of the following post really matters for certification, so feel free to ignore it - Jim] So, does maha only refer to herself in the third person then? How about a compromise: the question said we should use an instance of the outer class (even though it's not necessary since the inner class in static), but they said nothing about having to use an instance of the inner class (also not necessary, since the variable is static). So: code: Sorry, I just can't help nitpicking. P.S. ...and where did " [This message has been edited by Jim Yingst (edited March 10, 2000).]
|
maha anna bartender |
posted March 10, 2000 11:42 AM
Jim, The Object class has a protected method finalize. The declaration is protected void finalize() throws Throwable also you can define another method called in any class static void classFinalize() throws Throwable { . . . } regds
|
Jimnap unregistered |
posted March 10, 2000 11:56 AM
As per Jim's code , I get a compiler error as follows: No variable StaticInner defined in class Test at the line Without instantiating StaticInner, can I not access it's variable thru' the outer class instance? Jim, Anna Help....
|
maha anna bartender |
posted March 10, 2000 12:18 PM
Jimnap, Yes. You are right. As for as I know, we have to create an instance of the inner class, in this situation. Jim's code is not correct. Jim, [This message has been edited by maha anna (edited March 10, 2000).]
|
Jim Yingst sheriff |
posted March 10, 2000 12:54 PM
Ah, we seem to have stumbled into a difference between jdk 1.2.2 and jdk 1.3 beta. My code compiles fine using 1.3, but not with 1.2.2. Interesting. I had just assumed that since both the inner class and the variable were static, we shouldn't need an instance of either OuterClass or InnerClass to access the variable, but we could provide one or the other or both. It turns out that 1.2.2 doesn't support the syntax I thought it would, though. Naturally I regard this as a bug in 1.2.2, but I suppose that's a matter of opinion. Here's an alternative that does compile under 1.2.2, does use an instance of the outer class, and does not use an instance of the inner class: code:public class Test { As for my mood - no, I'm fine. I was just teasing you. I couldn't see any reason you needed the "throws Throwable" in the main() declaration in the code you posted, so I assumed you had left it there accidentally from a previous test program. (Something I do frequently.) Also I couldn't see why you'd prefer "maha's method" to "my method" - to me they'd both mean the same thing. Since you post so much accurate info here I have to see if I can catch you in any errors, however small or silly. Feel free to ignore me when I do that sort of thing.
|
| | |