• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt with static final field

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 !
 
jagan kay
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
jagan kay
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :

 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Madhukar Ojha
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic