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.

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:

 


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


final float PI = 3.14;

 

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?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

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 *