Scanner class Methods in Java

Java - Constants, Static & Final Fields


Constants

Constants are very useful in programs where huge calculation depends on fixed values like ,π in mathematics.We can not use all mathematical symbols but we can use a fixed variable to store these values.These variables require no change in values.The value of constants remain same throughout the program.

For example:

double PI=3.1415926;

This is a simple constants.We can still change its value because it will behave like normal variable.

Lets learn how can we make it fixed and immutable?For that we will us final keyword.

final Fields

The final keyword is used to make the variable or method immutable.This keyword is used when we want to create a fixed value. If a constant is declared as final then we will not be able to change its value later in program.

For example:  

final double PI=3.1415926;

now , we cannot override the value of PI.

Static Fields

We already discussed about static methods in java.Like, static methods we also have liberty to create static fields.

The static keyword can be used with fields like variables and constants. By using it with these fields we can make them static fields.The main advantage of using static with constants is that we can access that constant from any other class. It means we do not need to define constants in different classes.It gives the variables and constants a global scope.We can use final keyword with static keyword to make fields immutable and globally accessible.

Static fields is used to create members of class by declaring them static so that we can use them anywhere without changing their values. We can say static fields are used to declare constant and literals. 

For example:

final static double PI=3.1415926;

How static fields works?

As we know static methods do not require any object to access them.In the same we can use static fields in other classes without using any obj to access them.

static and final fields example
Working of Static fields 


Example: This example will demonstrate the use of static and final fields in a java program. 

Write a program to find area and circumference of circle using a static and final field PI.

Write a program to find area and circumference of circle using a static and final fields PI.
Example of static and final constant

In the above example, 

  1. First class is Circle which does not contain main method, it is just to declare a static final field PI. Because it is a constant value.
  2. Second class is CircleArea to get the area of class. We are importing static field using classname.Fieldname as Circle.PI. This is the static import which allow us to access any fields of Circle class by this class.
  3. Third class in Circumference class it is working in same way.We use two main classes to show how widely we can use the static variable.

Note: All classes are created separately in IDE. Create each class one by one and to see output run them individually.


Output of CircleArea class:

output of area and circumference of circle using a static and final fields PI.

Output of Circumference class:

output of area and circumference of circle using a static and final fields PI.

This way we can define any variable or constant as static fields.Try it Yourself! Happy Coding!

Comments