class A is the outer class. Class B is an inner class of A, and class C is an inner class of B.
All three classes declare and initialize one static final variable.
This is the object in class A:
static final Object x = new Object();
class B:
static final Object x1 = new Object();
class C:
static final Object x2 = new Object();
The problem is that the variable in class A compiles fine, but in B and C the variable does not.
Error message:
the field can not be declare static; static field only declared in static and top level type
The String
, and int
in class B and C doesn't receive an error.
Complete code:
class A {
static final Object x = new Object();
static final String s = "s1";
static final int y = 1;
class B {
static final Object x1 = new Object();
static final String s1 = "s1";
static final int y1 = 1;
class C {
static final Object x2 = new Object();
static final String s2 = "s1";
static final int y2 = 1;
}
}
}