Nick Silicon Nick Silicon - 1 year ago 76
Java Question

Do we necessarily need to initialize value of class contant in Java?

class Test
final double X ;
public static void main(String[] arg2)


Above code produces error:

variable X not initialized in the default constructor

But if we write
final double X;
method it is not error. So, is it necessary to initialize value of
final class variable

Answer Source

Any final fields must be initialized in all the constructors of your class otherwise you get a compilation error. Moving final double X; inside main(), is a totally different concept as it will then be a local variable whose value cannot be modified.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download