In java there is no such word like constant to define a variable as constant. But java has a keyword called final which is used to declare constant in Java. Like C in java also by declaring a variable as a constant cannot be modified further and if tried then it will give compilation error. Final is also called as the modifier in java like static is a modifier.
Syntax of final variable :
Important points regarding final variable :
Now think why it is not giving compilation error as we are very sure that final variable must be initialized at any cost because value once assign cannot be modified ?
4 > If we try to modify it then it will give the compilation error as " variable might already have been already assigned " . For ex-
Now you must be wonder how do we confirm the above written statement ?
6 > We can also use final with static with a variable to make it a class variable.
Syntax of final variable :
< final > < data_type > < var_name > = < value > ;
for ex- final int x = 20 ;
Important points regarding final variable :
1> As it is a constant in java so it must be initialized while declaring. If it is not initialized and tried to access then it will give run-time error as " variable might not have been initialized " . For ex-
public class prog6{
public static void main(String sush[]) {
final int x ; // final variable not initialized
System.out.println(x); // Run-time error
}
}
Now think why it is not giving compilation error as we are very sure that final variable must be initialized at any cost because value once assign cannot be modified ?
2 > It is not giving compilation error because if we are not assigning at the time of declaration then there is a chance of initializing it later in the method block but it must be before using it. For ex-
public class prog6{
public static void main(String sush[]) {
final int x ; // final variable not initialized
x = 40 ;
System.out.println(x);
}
}
3 > But the above two statement is only true when we are going to use a local constant. If it is instance local then we must have to initialize at the time of declaration otherwise it will give compilation error as "variable might not have been initialized ".
Why its a compilation error ?
It is because instance variable is by default initialized to zero and compiler is not sure that the programmer will surely initialize it later before using it but the same was not in the same of local variable because its mandatory to initialize local variable as it has not default value. So compiler is not going to take any risk and force the programmer to initialize. For ex-
Why its a compilation error ?
It is because instance variable is by default initialized to zero and compiler is not sure that the programmer will surely initialize it later before using it but the same was not in the same of local variable because its mandatory to initialize local variable as it has not default value. So compiler is not going to take any risk and force the programmer to initialize. For ex-
public class prog41{
final int x;
x=20; // Compilation Error
public static void main(String sush[]) {}
}
4 > If we try to modify it then it will give the compilation error as " variable might already have been already assigned " . For ex-
public static void main(String sush[]) {
final int x =40 ; // Initialized final variable
System.out.println(x++); // Compilation error
}
5 > If we try to use any arithmetic operation with the final variable while accessing it, it won't give any compilation as because we are not modifying the constant rather we are just accessing a constant value with another. For this, compiler is having a important role . For ex-
public static void main(String sush[]) {
final int x =40 ; // Initialized final variable
System.out.println(x+20); // Here it is not modifying the final variable.
}
You must be thanking of why it is not giving any compilation error are we modifying final variable . Actually it seems modifying but in real we are not modifying. But how ?
Whenever we are trying to access any final variable the task of the compiler is to put the direct assigned final value rather using it with variable. So in the above program compiler place 40 in place of " x " and now we left with " 40 + 20 ", hence we can add two constant easily .
Now you must be wonder how do we confirm the above written statement ?
Use any java decompiler it will decode your class file and we also know that .class file is generated by the compiler which is more optimized form of our source file . Here is the decompiled code of above program.
public class prog6 {
public static void main(String[] paramArrayOfString) {
System.out.println(60);
}
}
Here you can see that compiler just replaces the above code of " x + 20 " by 60 . So it is directly using value instead of using through variable.
6 > We can also use final with static with a variable to make it a class variable.
What is the use of making it a class scope ?
-- Defining final variable within a method will limit the accessibility within the method block only.
So what if we want to access outside the method ?
For this we have two option:
a> Using instance final variable i.e accessibility with the Object.
b> Using a class final variable i.e accessibility with class name no need of Object.
So if we have all member declared as a static in our program then no need to create an object for accessibility of members , we can use directly with the class name.
Note : Final static variable should not be declared within any method , it should be a class variable otherwise give compilation error " modifier static not allowed here".
0 comments:
Post a Comment