Java With Us
Home   |   Tutorial   |   Programs  

Java Tutorial

Introduction to Java
Hello World Program
Variables and Data types
More about data types
Displaying text using print and println
Displaying text using printf
Java Comments
Naming conventions for Identifiers
Mathematical operations in Java
Taking input from the user

Classes and Objects
Introduction to object oriented programming
The constituents of a Class
Creating objects and calling methods
Get and Set Methods
Default constructor provided by the compiler
Access Specfiers
Scope and lifetime of Variables
Call by value and Call by Reference

A few more topics
Class as a reference data type
Constants or literals
Final variables
Increment and decrement operators
Manipulating Strings
Overloading constructors and methods
Static methods and variables
The Java API
The Math class
this keyword
Wrapper classes

Control Structures
Control Statements
Repetition statements
Nested loops
Formulating algorithms
Branching Statements

Arrays introduction
Processing arrays using 1oops
Searching and sorting arrays
Array of objects
Multi dimensional arrays
Taking input as command line arguments
Using ellipsis to accept variable number of arguments

Inheritance introduction
Relation between a super class and sub class
Final classes and methods
The protected access specifier
Class Object

Abstract classes and methods

Exception handling
Exception handling introduction
Exception hierarchy
Nested try catch blocks
Throwing exceptions

Final Variables

We have seen what a variable is and we are also aware of the fact that a variable may be assigned a new value any number of times. That is the reason it is known as a variable i.e. something whose values keeps varying and does remains constant. Suppose that we need to use the value of PI repeatedly in our program correct to ten decimal places. Mentioning this value each time may not be a viable option. One thing that can be done is to initialise a variable named pi to its value in the following way in the beginning of the program and use the variable wherever the variable is required.

double pi = 3.1428571428;
double volume = pi * radius * radius * height;

But such a representation falsely implies that pi is a variable, something contradicting the fact that everyone is aware of. Moreover, one might erroneously change the value of the variable pi to a new value.

pi = 4.1458;

To avoid such mishaps, we have another type of variables in Java known as final variables. The value of such variables once initialised may not be changed later on during the execution of the program. Final variables are declared using the final modifier in the following way:

final double pi = 3.1428571428;

If we later on try to modify the value, as we have done earlier, compilation errors occur. So, marking a variable whose value shouldn't be changed helps it from being modified accidentally. This value of pi can be used just like any other value. In order to distinguish a final variable from a non final variable, we name it in all uppercase letters. If the variable name consist of more than a single word, they are separated by an underscore ( _ ) sign. This is the naming convention that everyone follows. For example:

double PI = 3.1428571428;
final int NUMBER_OF_TURNS = 34;

Final variables may be declared as either static or non static. However, it doesn't make any difference since the value of that variable can however not be changed. The only reason that could be cited for declaring a final variable as non static is to restrict its access only to objects of that class and avoid it from being accessed without creating its object.

Also, the value of a final variable should be initialised at the time of its declaration itself. For example, the statements below would result in errors:


The reason behind this is that all uninitialized variables are initialized by the compiler with their default values when creating objects of that class type. When the initialisation takes place to the programmer defined value, the task is equivalent to reassigning the variable with a new value.

Use of final variables also betters reflects the task being done and makes the program self explanatory. For example, look at the following statement:

double average = (double)( marks1 + marks2 + marks3 ) / 3;

At the first look the number 3 might not appear to be the total number of subjects. But if we use the following sets of statements, the expression becomes self explanatory.

double average = (double)( marks1 + marks2 + marks3 ) / NUMBER_OF_SUBJECTS;

Just like final variables, we also have final methods and final classes. The use of final with these entities also means the same as seen with variables. We shall look into it when we deal with inheritance.

Privacy Policy