aspose file tools*
The moose likes Java in General and the fly likes declaring an initialized local variable as final remove an unexpected error from the program. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

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

Deepak Kumar Jha
Greenhorn

Joined: Feb 07, 2013
Posts: 17

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

Joined: Feb 07, 2013
Posts: 17

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

Joined: Apr 06, 2010
Posts: 4363
    
    8

Duplicate of http://www.coderanch.com/t/604353/java-programmer-SCJP/certification/Confusing
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: declaring an initialized local variable as final remove an unexpected error from the program.