Explain the Final Keyword in Java.
May 22nd, 2008- A java variable can be declared using the keyword final. Then the final variable can be assigned only once.
- A variable that is declared as final and not initialized is called a blank final variable. A blank final variable forces the constructors to initialise it.
- Java classes declared as final cannot be extended. Restricting inheritance!
- Methods declared as final cannot be overridden. In methods private is equal to final, but in variables it is not.
- final parameters – values of the parameters cannot be changed after initialization. Do a small java exercise to find out the implications of final parameters in method overriding.
- Java local classes can only reference local variables and parameters that are declared as final.
- A visible advantage of declaring a java variable as static final is, the compiled java class results in faster performance.
A discussion inviting controversy on java final keyword:
‘final’ should not be called as constants. Because when an array is declared as final, the state of the object stored in the array can be modified. You need to make it immutable in order not to allow modifcations. In general context constants will not allow to modify. In C++, an array declared as const will not allow the above scenario but java allows. So java’s final is not the general constant used across in computer languages.
A variable that is declared static final is closer to constants in general software terminology. You must instantiate the variable when you declare it static final.
Definition as per java language specification (third edition) – 4.12.4 is “A final variable may only be assigned to once.”(§4.1.2)
Java language specification tries to redefine the meaning of constant in the following way!
We call a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28) a constant variable. Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1, §13.4.9) and definite assignment (§16).



can you send me some of the sample program in java to explain java final
please send me my mail id to what is final key word? and example java program esay to understand
java final nicely explained
thanks for the explanation on final keyword in java. please send me more about applet
please send to my mail id some of the sample programs of java using final keyword.
I’ve used variations of question on java final on many interviews – it’s surprising how many head-scratchings you get from something like:
private final List list = new ArrayList();
list.add(“value”);
– does the above work? (e.g. does final keyword make the java collection immutable) or other variations like declaring method inputs final, etc.
exelent defination for java final
jason – private final List list here you are just making a reference list as final, the class ArrayList is not immutable so to have a immutable class you have to have all references immutable too…making it just final won’t make it immutable.
Brief, to-the-point article. Thanks!
Hope this code helps:
class Another
{
final int xyz;
public Another()
{
xyz=1000;
}
public void display()
{
System.out.println(“XYZ=”+xyz);
}
}
public class FinalTest
{
static final int y=3;
public FinalTest()
{
}
static
{
System.out.println(“Just for fun…”);
}
public static void main(String[] args)
{
final int x=5;
System.out.println(“X=”+x);
System.out.println(“Y=”+y);
Another ano=new Another();
ano.display();
}
}
Good desc on java final keyword.. very useful .. :)
Simple explanation …easy understandable…
its really good stuff on one desk to get on for beginer’s knowledge
can any one tel me for how to write a program for multiplication of 200 numbers wid another 200 numbers….plz……help or either giv some basic idea for hw to make dis program …m nt geting it
gud….got basic information about final keyword from it……thank u….
thanks guys.
firstly thanks on the explanation on java final keyword. because u explain terms in jsp and servlet very simple language.
please tell me about scriptless jsp.
very nicely explain…thanksss
I want to java program examples on final keywords