Scenario3: Final local Variables
There can be scenario where we can use final keyword with the local variables.
There can be scenario where we can use final keyword with the local variables.
package in.technoscience.finalasconstant;
1.public class FinalVariables {
2. public static String someMethod(final String environmentVariable){
3. final String pathVariable= "env."+environmentVariable;
4. System.out.println("pathVariable is"+pathVariable);
5. return (System.getProperty(pathVariable));
}
}
Here we have used final variable inside the method means as a local variable.In this case, the final variable is final only within the execution of method, which is different at each
execution of the method. Each time the control enters inside method, the final is
reconstructed and its can not change during the execution of the method.
package in.technoscience.finalasconstant;
public class TestingFinalVariable {
public static void main(final String[] args) {
System.out.println("note the change in path variable after method invocation:");
FinalVariables.someMethod("JAVA_HOME");
FinalVariables.someMethod("MAVEN_HOME");
}
}
Each time the method is entered, the passed-in environmentVariable parameter is appended to the constant prefix and then fix for the duration of the method call. So why make the variable final? Because once this variable is set in the body of the method, it cannot be changed.
There may be below situation where not using the final can create problem:
package in.technoscience.finalasconstant;
public class FinalVariable {
public static String wrongMethod(final String environmentVariable){
final String path="Path_Variable :"+environmentVariable;
System.out.println("path variable is:"+path);
// path= new String("may be different value"); compiler unhappy
return System.getenv(path);
}
}
In this example, compiler was unhappy because I was trying to reassign path to a different value. But as we have used the final keyword, the
compiler tells us that an error was made.
The technique of fixing variables with final become useful for long or complicated methods that have many local variables.
Note:
Method-scoped final variables aren't the same as constants even they behave like constant during method execution.
Method-scoped final variables aren't the same as constants even they behave like constant during method execution.
The difference is that method-scoped
final variables are variable. Each time
the method is entered, their values are changed based on the needs of that method execution.
However, method-scoped constants always have the same
values regardless of the circumstances under which the method is run.
Scenario4:
package in.technoscience.finalasconstant;
1.public class ChainingOfFinals {
2. public final String name;
3. public final int nameLength = this.name.length();
4. // public final int nameLength = name.length();// compiler unhappy
5. // public final String anotherValue = name; // compiler unhappy
6. public ChainingOfFinals(final String name) {
7. this.name = name;
}
}
In this code,
line no 3 will work properly, final variable name must be initialized in the constructor of class Therefore, variable nameLength can take advantage of name when the instance is initialized. However, we must use the this keyword in front of the variable name. If we don't, it won't compile. we can visualize this at line no 4.
At line no 4 and 5 compiler will issue an error as follow:
The blank final field name may not be initialized.
....... To be continued in next post, keep visiting
No comments:
Post a Comment