IF only Life were that simple..
(SIGH...:-) )
My compiler screams at the static variables in C and B in the following code:
**********************************************************
public class testinner {
static int i=200;
static int j=300;
static class A{ A(){ System.out.println(i++); } }
class B{ static int i;B() {; System.out.println(i++); } }
class C{ static int i=50; C() { System.out.println(i++);System.out.println(testinner.this.i++); } }
public static void main(
String args[]) {
A a = new A();
testinner.B b =new testinner().new B();
C c = new testinner(). new C();
A a1 = new A();
}
}
**********************************
The noise it makes I reproduce below:
C:\java>javac testinner.java
testinner.java:8: Variable i can't be static in inner class testinner. B. Only
members of interfaces and top-level classes can be static.
class B{ static int i;B() {; System.out.println(i++); } }
^
testinner.java:10: Variable i can't be static in inner class testinner. C. Only
members of interfaces and top-level classes can be static.
class C{ static int i=50; C() { System.out.println(i++);System.out.println(testi
nner.this.i++); } }
^
2 errors
Besides, why doesnt it complain about the expression
"C c = new testinner(). new C();"
Shouldnt it be" testinner.C=..."
as it is being called from main?/
Java 1.2.2 is the compiler i am using
Stumped...please can someone clarify things as they stand regarding inner classes, in the context of
SCJP??
(i.e) Do I take my Compiler's behaviour as normal and believe staunchly that member inner classes can have static member variables or do I follow the general belief(which might, for all I know, be wrong), as proved by my fellow greenhorn's assertion that it is legal, that they can??
And why is the Creation of a C object without using the exprtession "testinner.C=.." not objected to by the compiler?/
Thanks for your replies in advance..
RCT