Nick Silicon Nick Silicon - 8 months ago 35
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


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.