Constant
A constant is also an identifier whose value cannot be changed during execution of the program. In java programming in order to make any thing as constant, we use keyword called final.
final is a keyword which is placing an important role in java programming in 3 places they are
1. At variable level
2. At method level
3. At class level
final at variable level: -
If we don’t want to change the value of the variable then the variable must be made as constant by using final keyword.
Syntax: -
Final data type v1=val1, v2=val2 …… vn=valn;
Ex: -
final int a = 100;
a = 200 // invalid
final float PI = 3.1417;
PI=PI + 1; // invalid
final variables cannot be modified/cannot be changed.
final at method level: -
We know that each and every operation must be performed with respect to methods/functions in any programming language that is without methods concept we cannot perform any operations.
In software development if we define any method which is common for many number of java programmers then such methods can be used by every java programmer, for they should not change the definition, such type of methods are known as Constant methods and their definition must be preceded by final keyword.
Syntax: -
final return type method name ( list of formal parameters if any)
{
Block of statements (s);
}
Ex: -
Write a method for calculating simple intrest SI= (P*T*R)/100
final float simpleIntrest (float P, floatT, floatR)
{
Float SI = (P*T*R)/100;
return(SI);
}
final is a keyword which is placing an important role in java programming in 3 places they are
1. At variable level
2. At method level
3. At class level
final at variable level: -
If we don’t want to change the value of the variable then the variable must be made as constant by using final keyword.
Syntax: -
Final data type v1=val1, v2=val2 …… vn=valn;
Ex: -
final int a = 100;
a = 200 // invalid
final float PI = 3.1417;
PI=PI + 1; // invalid
final variables cannot be modified/cannot be changed.
final at method level: -
We know that each and every operation must be performed with respect to methods/functions in any programming language that is without methods concept we cannot perform any operations.
In software development if we define any method which is common for many number of java programmers then such methods can be used by every java programmer, for they should not change the definition, such type of methods are known as Constant methods and their definition must be preceded by final keyword.
Syntax: -
final return type method name ( list of formal parameters if any)
{
Block of statements (s);
}
Ex: -
Write a method for calculating simple intrest SI= (P*T*R)/100
final float simpleIntrest (float P, floatT, floatR)
{
Float SI = (P*T*R)/100;
return(SI);
}
Note: -
In java programming if we are writing any method name and if it is containing one word (or) more than one word then first word, first letter is small and the rest of subsequent words first letters must be capital.
Ex: -
itemStateChanged ()
actionPeformed ()
textValueChanged ()
Here final methods of java cannot be overridden/cannot be redefined.
Final at class level: -
If we don’t want to give features of base class to the derived class then the definition of the base class must be made as final.
final classes never participates in Inheritance/reusable.
In java programming if we are writing any method name and if it is containing one word (or) more than one word then first word, first letter is small and the rest of subsequent words first letters must be capital.
Ex: -
itemStateChanged ()
actionPeformed ()
textValueChanged ()
Here final methods of java cannot be overridden/cannot be redefined.
Final at class level: -
If we don’t want to give features of base class to the derived class then the definition of the base class must be made as final.
final classes never participates in Inheritance/reusable.
Visitors