/*
RULE: we can declare instance variable as final but jvm wont provide any default values for instance variable, programmer have provide the values for final instance variables .
we can provide values in three ways
1. at the time of declaring the variable.
2. in constructor.
3.in instance block
*/
public class Final_Instance_Variable
{
final double d;
public static void main(String ar[])
{
System.out.println(x); // compile time error
}
}
Way 1 : initialization value for final instance variable
public class Final_Instance_Variable
{
final double d=20.3;
public static void main(String ar[])
{
System.out.println(x);
}
}
Way 2 : initialization value for final instance variable
public class Final_Instance_Variable
{
final double d;
public Final_Instance_Variable()
{
d=20.3;
}
public static void main(String ar[])
{
System.out.println(x);
}
}
Way 3 : initialization value for final instance variable
public class Final_Instance_Variable
{
final double d;
{
d=20.3;
}
public static void main(String ar[])
{
System.out.println(x);
}
}
RULE: we can declare instance variable as final but jvm wont provide any default values for instance variable, programmer have provide the values for final instance variables .
we can provide values in three ways
1. at the time of declaring the variable.
2. in constructor.
3.in instance block
*/
public class Final_Instance_Variable
{
final double d;
public static void main(String ar[])
{
System.out.println(x); // compile time error
}
}
Way 1 : initialization value for final instance variable
public class Final_Instance_Variable
{
final double d=20.3;
public static void main(String ar[])
{
System.out.println(x);
}
}
Way 2 : initialization value for final instance variable
public class Final_Instance_Variable
{
final double d;
public Final_Instance_Variable()
{
d=20.3;
}
public static void main(String ar[])
{
System.out.println(x);
}
}
Way 3 : initialization value for final instance variable
public class Final_Instance_Variable
{
final double d;
{
d=20.3;
}
public static void main(String ar[])
{
System.out.println(x);
}
}