Results 1 to 5 of 5
Thread: variable initialization problem
- 07-26-2011, 06:52 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 59
- Rep Power
- 0
variable initialization problem
Hi guys.........
I have a small problem, basically fundamental problem.
public class Compile15 {
public static void main(String args[]){
int i1=9; //3
int i2; //4
if(i1>3){
i2=8;
}
System.out.println(i2);
}
}
In this above program when I try to run this program, it occur compile time error -- "variable i2 might not have been initialized" ,although within if condition I have initialized i2.
Now if I initialize i2=0 at line 4 it runs perfectly or at line 3 if I declare variable i1 as final int i1 without changing at line 4 then it will compile and run also.
I'm not so cleared about these two solutions. Please someone clarify this for me in details.
Thank you................
- 07-26-2011, 07:00 AM #2
The line i2=8 is not initialisation. It is plain old assignment. Initialisation is giving a variable an initial value when you declare it. For local variables you must intialise them yourself. Whereas for instance variables if you do not initialise them they are intialised by the compiler with a default value.
So why does the compiler complain? What happens when the if statement is false? What value does i2 have then?
- 07-26-2011, 07:02 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
The compiler doesn't do any interpretation on (the source of) your problem. It just checks that i2 is only initialized if an if-condition succeeds; if it doesn'out succeed, that variable isn't initialized; you, on the other hand, do perform some form of interpretation and you can conclude that that variable must always be initialized because that if-condition always succeeds. If you make that other variable final, the compiler 'knows' the value of that variable and it can deduce that the if-condition succeeds and your variable i2 is always initialized (so it doesn't complain).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-26-2011, 07:09 AM #4
Lets consider several other versions of your code.
For the first 2 versions we can see that the if statement will not be entered. With the 3rd version we have no idea if it will enter or not unless we have the source of the method as well. Now what the compiler needs to do is have consistent behavior across all 3 versions as well as yours. I certainly would not trust a compiler if it behaves differently just because you want it to.Java Code:// version 1 int i1=9; int i2; i1 = 0; if(i1>3){ i2=8; } System.out.println(i2); // version 2 int i1=0; int i2; if(i1>3){ i2=8; } System.out.println(i2); // version 3 int i1=9; int i2; i1 = 0; if(someMethodThatReturnsBoolean()){ i2=8; } System.out.println(i2);
- 07-26-2011, 07:22 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 59
- Rep Power
- 0
Similar Threads
-
return new variable -problem
By Hevonen in forum New To JavaReplies: 7Last Post: 12-08-2008, 06:07 AM -
Stack class/problem in stackSize Initialization and usage of pop() function
By Mazharul in forum New To JavaReplies: 1Last Post: 11-17-2008, 09:32 AM -
variable initialization
By rqal.10 in forum New To JavaReplies: 5Last Post: 06-15-2008, 06:39 AM -
initialization value problem
By ravian in forum New To JavaReplies: 2Last Post: 01-28-2008, 10:54 AM -
Icon initialization problem
By saz25 in forum AWT / SwingReplies: 1Last Post: 12-24-2007, 10:37 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks