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 to respond me. Following is the compiler error when i compiled the 1st program from the command prompt. When i tried to run it from the Eclipse Editor it gives the 'Unresolved compilation problem'.
MY DOUBT IS: why an uninitialized constant of an interface doesn't have its default value when an uninitialized constant of an abstract class does ? Do unintialized static fields of both concrete and abstract class have their default value ? But why it is not in an interface ?
All variables declared in interface is implicitly 1. public 2. static 3. final
A static final variable must be declared at the time of creation or static block . you have neither initialized it at declaration nor in static block . That is why compiler is complaining .
SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
Joined: Jun 08, 2008
Hi Madhukar Ojha,
I know by default all the fields of an interface is public static final. I just wanted to make a comparision with the variable 'b'(static final int) in abstract class 'Abs'.
Pl, correct me if i'm wrong... "all the uninitialized variables or constants either in a concrete class or an abstract class will have their default value". Is that correct?
And please look at my second program. It compiles and prints the value of both 'a' & 'b'. Here, the uninitialized constant 'b'(static final) of the abstract class gets and prints value zero. If i once uncomment the commented line marked by '*' i get compilation error. I can't find the problem.
for me static final int field in abstract class also giving same error as in interface. I think, static final int fields needs to be initialized when declared in interface. and if we declare them in class they can be initialized in static block as well.
SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Joined: Mar 21, 2007
You must keep in mind that fianl variables whether static or non-static do not get default values . You must have initialized them otherwise compiler complains .