without giving value. The final value depends upon the value supplied by
user. Suppose the if condition is true, then the final variable "s" is
assigned then cant be reassigned. Method-scoped final variable are not same
as a constant. This final variable expecting to assing value at the first
usage. Final variable should be assigned next usage.
public class just{
public static void main(String[] arg){
final String s ;
if(arg[0].equals("a")){
s = "a";
}
else if(arg[0].equals("b")){
s="b";
}
if(arg[0].equals("c")){
s = "c"; // <- Compiler Error that already assigned.
//But still not assigned if arg[0]="w"
}
}
}
Correct code :
public class just{
public static void main(String[] arg){
final String s ;
if(arg[0].equals("a")){
s = "a";
}
else if(arg[0].equals("b")){
s="b";
}
else if(arg[0].equals("c")){
s = "c"; // <- No Error
}
}
}
-----------------------------------------------
Here the non-final method variable. Can be assigned at point before
variable going to use.
public class code{
public static void main(String arg[]){
String s;
int i;
if(arg[0].equals("a")){
s="a";
}
else if(arg[0].equals("b")){
s="b";
}
if(arg[0].equals("c")){
s ="c";
}
}
}
This e-Mail may contain proprietary and confidential information and is sent for the intended recipient(s) only. If by an addressing or transmission error this mail has been misdirected to you, you are requested to delete this mail immediately. You are also hereby notified that any use, any form of reproduction, dissemination, copying, disclosure, modification, distribution and/or publication of this e-mail message, contents or its attachment other than by its intended recipient/s is strictly prohibited.
Visit us at http://www.polaris.co.in
No comments:
Post a Comment