Java Final Keyword

22/05/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

serin on December 19th, 2009 4:36 pm

please send me my mail id to what is final key word? and example java program esay to understand

vivek on December 21st, 2009 2:41 pm

java final nicely explained

gurjit on December 28th, 2009 1:27 pm

thanks for the explanation on final keyword in java. please send me more about applet

khalid ali on January 19th, 2010 5:55 am

please send to my mail id some of the sample programs of java using final keyword.

sanjay on January 26th, 2010 6:12 pm

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.

Jason Shao on February 12th, 2010 4:09 pm

exelent defination for java final

Anonymous on February 23rd, 2010 3:23 pm

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.

puran on March 19th, 2010 6:56 pm

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();
}

}

MiJ on March 31st, 2010 4:43 am

Good desc on java final keyword.. very useful .. :)

naveen on April 19th, 2010 3:45 pm

Simple explanation …easy understandable…

srinivas on May 10th, 2010 11:05 am

its really good stuff on one desk to get on for beginer’s knowledge

rahul singh on May 12th, 2010 8:58 am

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

hitasha on May 23rd, 2010 3:01 pm

gud….got basic information about final keyword from it……thank u….

anusha on June 2nd, 2010 1:33 pm

thanks guys.

Joe on June 2nd, 2010 2:22 pm

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.

pankaj on June 26th, 2010 10:24 am

very nicely explain…thanksss

debojit dey on July 16th, 2010 8:32 pm

I want to java program examples on final keywords

durgaprasad on July 17th, 2010 5:50 am

please send to my mail id.in final keywords,class variable methods using.pls send to the some example program for this three types. separare example this three words

saranya on August 10th, 2010 3:12 pm

why could an object in java could not be declared final.

kushal on August 13th, 2010 4:40 am

talk.to.shan.now@gmail.com
Send all your problems here..
@HITASHA
import java.io.*;
class mul{
int a=new int[400];
mul()
{}
void mul1()
{
DataInputStream c=new DataInputStream(System.io);
System.out.print(“Enter 1st 200 numbers..\n”);
for(int i=0;i<200;i++)
{
System.out.print("\nEnter"+i+1+" no:");
int p=Integer.parseInt(c.readLine());
a[i]=p;
}
System.out.print("\n\nEnter Next 200 numbers..\n");
for(i=200;i<400;i++)
{
System.out.print("\nEnter"+i+1+" no:");
int p=Integer.parseInt(c.readLine());
a[i]=p;
}
for(i=0;i<400;i++)
{
int s=a[i]*a[i+200];
System.out.print("\nMultiplication of "+i+1+" and "+i+201+" is:"+s);
}
}

public static void main(String arg[]){

santanu on September 6th, 2010 4:21 am

talk.to.shan.now@gmail.com
Send all your problems here..
@HITASHA
import java.io.*;
class mul{
int[] a=new int[400];
mul()
{}
void mul1()
{
try{
DataInputStream c=new DataInputStream(System.in);
System.out.print(“Enter 1st 200 numbers..\n”);
for(int i=0;i<5;i++)
{
System.out.print("\nEnter no "+(i+1)+":");
int p=Integer.parseInt(c.readLine());
a[i]=p;
}
System.out.print("\n\nEnter Next 200 numbers..\n");
for(int i=5;i<10;i++)
{
System.out.print("\nEnter no "+(i+1)+":");
int p=Integer.parseInt(c.readLine());
a[i]=p;
}
for(int i=0;i<5;i++)
{
int s=a[i]*a[i+5];
System.out.print("\nMultiplication of no "+(i+1)+" and no "+(i+6)+" is :"+s);
}
}
catch(IOException e)
{}
}
public static void main(String arg[]) throws IOException{
mul shan=new mul();
shan.mul1();
}}

Shan on September 6th, 2010 4:33 am

talk.to.shan.now@gmail.com
Send all your problems here..
@HITASHA
import java.io.*;
class mul{
int[] a=new int[400];
mul()
{}
void mul1()
{
try{
DataInputStream c=new DataInputStream(System.in);
System.out.print(“Enter 1st 200 numbers..\n”);
for(int i=0;i<5;i++)
{
System.out.print("\nEnter no "+(i+1)+":");
int p=Integer.parseInt(c.readLine());
a[i]=p;
}
System.out.print("\n\nEnter Next 200 numbers..\n");
for(int i=5;i<10;i++)
{
System.out.print("\nEnter no "+(i+1)+":");
int p=Integer.parseInt(c.readLine());
a[i]=p;
}
for(int i=0;i<5;i++)
{
int s=a[i]*a[i+5];
System.out.print("\nMultiplication of no "+(i+1)+" and no "+(i+6)+" is :"+s);
}
}
catch(IOException e)
{}
}
public static void main(String arg[])throws IOException{
mul shan=new mul();
shan.mul1();
}}

Shan on September 6th, 2010 4:33 am

there was some posting problem..
Check this is the FINAL one..

import java.io.*;
class mul{
int[] a=new int[400];
mul()
{}
void mul1()
{
try{
DataInputStream c=new DataInputStream(System.in);
System.out.print(“Enter 1st 200 numbers..\n”);
for(int i=0;i<200;i++)
{
System.out.print("\nEnter no "+(i+1)+":");
int p=Integer.parseInt(c.readLine());
a[i]=p;
}
System.out.print("\n\nEnter Next 200 numbers..\n");
for(int i=200;i<400;i++)
{
System.out.print("\nEnter no "+(i+1)+":");
int p=Integer.parseInt(c.readLine());
a[i]=p;
}
for(int i=0;i<200;i++)
{
int s=a[i]*a[i+200];
System.out.print("\nMultiplication of no "+(i+1)+" and no "+(i+201)+" is :"+s);
}
}
catch(IOException e)
{}
}
public static void main(String arg[]) throws IOException{
mul shan=new mul();
shan.mul1();
}}

Shan on September 6th, 2010 4:37 am

when i declare class level variable
in class hello
final static int h=1;

after that one main class created,(test)
then we call in main
hell0.h;

then hello class loaded or not?????

manish kumar on September 17th, 2010 2:50 pm

The Confusing Final and Volatile keyword has been changed in New memory model after JDK1.5 and have been included in Visibilty concept to know more read :-

https://www.ibm.com/developerworks/library/j-jtp03304/

Lokesh on September 20th, 2010 6:28 am

/* Check this is the FINAL one….. */

class Circle
{
protected float radius;
public static final float PI=3.14f;
public Circle()
{
radius=0f;
}
public Circle( float radius)
{
this.radius=radius;
}
public void setData(float radius)
{
this.radius=radius;
}
public void showData()
{
System.out.println(“radius is:”+radius);
}
public final float getArea()
{
return PI*(float)Math.pow(radius,2);
}
public static void main(String args[])
{
Circle c1=new Circle();
c1.setData(5f);
c1.showData();
}
System.out.println(“Area is:”+c1.getArea());
}

Md. Murad Hussain on November 8th, 2010 12:40 pm

very use full for programmer

sonu on November 11th, 2010 6:49 am

great!!!!!!!!it’s the very useful note for comparison to any other reference books…

kapS on December 8th, 2010 2:47 am

Java classes declared as final cannot be extended. Restricting inheritance!

here
what’s mean of “Restrictinginheritance!”

preeti on December 13th, 2010 5:15 pm

class jlc{
public static void main(String as[]){
final int x=10;
byte b=x; System.out.println(b);
}
}
can any one explain how typecasting is possible and memory representation of final keyword.

Saurabh on December 17th, 2010 10:18 am

Thanks very much Joe for such easy and nice explanation. I have a query though.

Could you explain how to make an object reference constant as well as the object assigned to it immutable? I suppose this is possible but still is a puzzel to me.

Thanks in advance,
Satish

Satish on December 27th, 2010 8:57 am

Email:

about
I am Joe, author of this blog. I run this with loads of passion. If you are into java, you may find lot of interesting things around ...more about me. Google+
java badge
Home