hi all,
interface A{int a;}
public class IfVarTest implements A{
public static void main(
String... s){
System.out.println("A.a="+a); // Generates compiler error
System.out.println("last line of 'main()'");
}
}
result:
Exception in
thread "main" java.lang.Error: Unresolved
compilation problem: The blank final field a may not have been initialized
-------------------------------------------------------------------------
abstract class Abs{
static int a;
static final int b;
static void displayB(){
System.out.println("Inside class 'Abs.display()' & b="+b);
}
}
public class StatInAbsClsTest extends Abs{
public static void main(String... s){
System.out.println("Abs.a="+a);
System.out.println("Abs.b="+b);
//displayJ(); // * (Generates compiler error)
}
}
result:
Abs.i=0
Abs.j=0
------------------------------------------------------------------------
i'm confused why a constant an interface doesn't have its default value
when an uninitialized constant of an abstract class does ?
As per K&B, all the uninitialized static variables of a class have its
default values like instance varibles.
So, i suppose even if class 'Abs' is an abstract class, it is a class. Hence, all the uninitialized static fields(varibles & constants) will have their default values. But it is not in the case with an interface. If i'm mistaken, Pl correct me if any one finds time.
More over if the line commented by '*' is uncommented compiler error encounters. Pl., clarrify that too if possible.
Thanks in advance.
jagan.k