how it can be possible that we can initialize the final variable of the class at the creation time of the object ?
Anybody can explain it how is it possible ? ...
See Question&Answers more detail:oshow it can be possible that we can initialize the final variable of the class at the creation time of the object ?
Anybody can explain it how is it possible ? ...
See Question&Answers more detail:osYou must initialize a final variable once and only once. There are three ways to do that for an instance variable:
Here is an example of all three:
public class X
{
private final int a;
private final int b;
private final int c = 10;
{
b = 20;
}
public X(final int val)
{
a = val;
}
}
In each case the code is run once when you call new X(...)
and there is no way to call any of those again, which satisfies the requirement of the initialization happening once and only once per instance.