This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt with static final field Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt with static final field" Watch "doubt with static final field" New topic
Author

doubt with static final field

jagan kay
Greenhorn

Joined: Jun 08, 2008
Posts: 22
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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

"Unresolved compilation problem" often means that your class files do not correspond to your java source files.

Try deleting all your class files and recompiling everything.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Originally posted by jagan kay:
hi all,

interface A{int a;}



One more thing , in interface complier wont initialized interface variables with default values , because they are implicitly public, static and final !


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
jagan kay
Greenhorn

Joined: Jun 08, 2008
Posts: 22
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'.

C:\Jagan>javac IfVarTest.java
IfVarTest.java:1: = expected
interface A{int a;}
^
1 error

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 ?
Madhukar Ojha
Ranch Hand

Joined: Mar 21, 2007
Posts: 71
Hi jagan ,

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 ~~♥๑۩۞۩๑
jagan kay
Greenhorn

Joined: Jun 08, 2008
Posts: 22
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.

Thanks in advance.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Hi Jagan ,

I compiled your code and found the following error :



I`m using JDK 1.5_14 , Which JDK version you are using ?

I compiled the following code :

ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

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.
Madhukar Ojha
Ranch Hand

Joined: Mar 21, 2007
Posts: 71
Hi Jagan

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 .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt with static final field
 
Similar Threads
Interface constant
an abstract class implementing an interface
java
don't use any IDE for SCJP
How it is possible abstract class have an object