Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
    Bookmark Topic Watch Topic
  • New Topic

declaring an initialized local variable as final remove an unexpected error from the program.

 
Deepak Kumar Jha
Greenhorn
Posts: 18
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
public class Test{
public static void main(String a[]) {
int i1=9;
int i2;
if(i1>3) {
System.out.println(i1);
i2=8;
}
System.out.println(i2);
System.out.println(i1);
}
}



this give an error message that local variable must be initialized.i.e i2
but when we write that code in such a manner


public class Test{
public static void main(String a[]) {
final int i1=9;
int i2;
if(i1>3) {
System.out.println(i1);
i2=8;
}
System.out.println(i2);
System.out.println(i1);
}
}


it does not show any error message . is there any property of final when it used for local variable or something else ..please share your view.
 
Deepak Kumar Jha
Greenhorn
Posts: 18
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
I am Beginner in java. I tried to run this program in eclipse IDE but it gives error when i write first part of program. but it does not show any error when i write second part only change is addition of final keyword. what happens to this code is not understandable by me . explain with reason.
 
Matthew Brown
Bartender
Posts: 4565
8
 
    Bookmark Topic Watch Topic
  • New Topic