Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Java Programming » Core Java Basics » final Keyword
Suryateja Pericherla Categories: Core Java Basics. No Comments on final Keyword
0
(0)

The final keyword in Java has various uses. In this article, we are going to look at only one of its uses. Other two uses of the final keyword are related to inheritance. So final keyword will be again revisited in the inheritance concept.

 

Primary use of the final keyword is to declare constants in Java programs. A constant is like a variable but, whose value cannot be changed once initialized. Syntax for declaring a constant using final keyword is shown below:

 

final data-type identifier = value;

 

You should remember that a constant should be initialized at the time of declaration itself. Once a constant has been initialized, it cannot be modified later. For example you can declare a PI constant as shown below:

 

final float PI = 3.14;

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


Java’s convention for constant names is that they should be written in uppercase. Below code segment  demonstrates the use of constants (created using final keyword) in a Java program:

class Circle
{
	float  radius;
	final  float  PI = 3.14;
	void displayArea()
	{
		float area;
		radius = 2.3;
		area = PI * radius * radius;
		System.out.println("Area of the circle is: "+area);
	}
}

 

There are two more uses of final keyword which will be discussed in inheritance section.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram