-1

My Code : A.java

package pack1;

public  class A {

    final static int x; 
}  

Command used to compile Code :

javac -d . A.java 

Compile Time Error :

A.java:5: error: variable x not initialized **in the default constructor**

    final static int x; 
                     ^
1 error

Now, here compiler should throw error like =>

error: variable x not initialized **in the static block**

Because here in this case possible place where you can initialize, the declared final static uninitialized variable are only => 1) at the time of declaration itself OR 2) inside static block

There is no constructor in the picture...still Compiler is throwing error which is misleading.

I am using jdk 1.8

java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
  • 2
    The static final can not be initialized in the constructor as it will try to assign value each time an object is created which is an error. Instead are you trying to use the variable x in the default constructor which the error is trying to say ? – OTM Jul 15 '17 at 18:07
  • 1
    Welcome to Stack Overflow! We are a question-and-answer site, not a bug repository. Please edit your question to make it clear what help you need. – Joe C Jul 15 '17 at 20:49
  • 1
    https://bugs.openjdk.java.net/browse/JDK-8184444 – Holger Jul 17 '17 at 07:25
  • Thanks a lot Holger.... !! – Akshay N. Shelke Jul 17 '17 at 08:27

2 Answers2

1

It is a bug in jdk.

https://bugs.openjdk.java.net/browse/JDK-8184444

Still unresolved ..

-1

When you declare a class in Java without explicitly declaring a constructor, Java creates an invisible default no-args constructor that looks like this:

public A() {}

Since your variable x is final, it needs to be given its value when it is declared or in the constructor.

As commenter OTM indicated, a static final variable should be initialized in its declaration, not in a constructor, since the constructor could be called any number of times (including zero).

I agree that the error is misleading to people who don't know about default constructors, but it does make a certain amount of sense.

Ellen Spertus
  • 6,576
  • 9
  • 50
  • 101
  • Why the downvote? If my answer is incorrect, please share why, so I (and other readers) can learn. – Ellen Spertus Jul 15 '17 at 23:11
  • 1
    variable x is final as well as static, so any type constructor will not be helpful in this case. So here we have only two choices=> 1) Assign value to x @ time declaration itself, OR 2) use "static" block for initialization... At both the places, if you are not assigning/initializing variable x then Compiler has to tell the proper error message.. like => error: variable x not initialized while declaration or in the static block. instead of this compiler is misleading with error like => error: variable x not initialized in the default constructor So I think its a bug ... :) – Akshay N. Shelke Jul 16 '17 at 14:40