why does java not allow static declarations with a static initialization block in a non-static inner class?
in the below code, outer2 will work and inner2 will not, despite doing the same thing. any ideas? i'm not looking for a workaround, i'm just trying to understand why java fails to do this.
public class WhyUNoStatic {
public static final String outer1 = "snth"; // ok
public static final String outer2; // ok
static
{
outer2 = "snth";
}
public class Inner {
public static final String inner1 = "snth"; // still ok!
public static final String inner2; // FAILURE TIME
static
{
inner2 = "snth";
}
}
}
edit: note that inner1
will work fine. it's not that java prohibits static vars in inner classes, it just prohibits declarations of them.