It's a good question. In both the ways, the variable x's scope is within the try block, first case and within both the catch block and try block in the second case. I tried to change x = 17 in the catch block but it still prints 7....Any reasons???
Thanks in advance and thanks Sanjeev for bringing this question.
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
When you try to compile the code, the compiler checks to see if any variables falls into the possibility of being an unreachable code.
Here though there is only one line in try block, but theoritically it falls in the category where this line may become unreachable (in case if some more line in the code exists above it ).
Thus compiler will report the error that x might not have been initialized.
Now when you initialize x in the catch block also than the compiler don't complain. B'cos now the compiler knows that in any case, even if some exception occurs, x will be initialized.
I hope this clears the doubt about compiler error. Now coming to Jothi's doubt. When you change the value of x in catch block, it matters only when some exception occurs and x is initialized through catch block. Under normal execution, x will always be initialized as 7 and value of Y will always be printed as 7.
To print value of x (ie value of y) as 17 try this code
Why this compiler is playing such a game??? I understood it but still I'm wondering and refusing to imagine that when we say x = 17 in the catch block the compiler is happy but it takes the value from within the try scope...
Hi Sanjeev , I am very happy that you asked this magic Q.I got solution.Here is only one concept that is, for compiler checking .At Compile time Compiler will check that when Developer is using try catch block, So compiler knows that that this uses for exception handeling.So, Compiler thinks that if there may exception in try block at run time then it may throw exception because runtime checking will be done by JVM not by compiler. So compiler will also check it in catch block. If it is there then in both cases x will initialise means if exception caught or if there is no exception.
i think like that . Thanks a lot To Sanjeev to point us on basic issue Prashant Singh
Joined: Nov 01, 2006
Hi all, What my understanding about the problem is as stated...... I think to understand this question it is neccessary to understand the actions which are going to happen in compile time and runtime activities. COMPILE TIME ACTIVITIES The compile will ensure the initializaion of the local variable at any cost.So when we initialze a non initialized local variable in a try block,though the try has a single statement,the compiler is feared about the non initializaion of the local varible as exception can occur at any statement which might be before or after the initializaion(but in reallity there is only one statement..compiler can not count the number of statement and its relative position w.r.t. other statement).After the re-initializaion of the variable in the catch block makes sures that even if there is any exception in the try block it will be initialized in the catch block....so initializaion will be done at any cost.
RUN TIME ACTIVITY There is no exception in the try block to be catched by the catch block.so the the value of x will be initialized the the try block initializaion.The catch will never be executed.
Hi Ranchers, I am going to explain Sanjeev Q' solution : 1) I am taking first case where code is like below:
In this case, when we will try to compile it then compiler will check for local variable x, because local variables must be initialized. So compiler comes in try block where it will find x=7; but compiler does not know about run time activity.So, compiler will think that if try block may have any exceptional statement then try block will not execute,So control will jump to catch block.So compiler will check catch block for confirmation that if any exception will arise ,then variable x will be initialise. So in above case in catch block there is no initialisation of x.So it throws compile time error. next case,If we take second case like..
In this case, we have initialized in both places as x=7. So it full fill all compiler's requirements.So it compile. Another confusion arises by some friends that he has initialized x=7 in try and x=19 in catch but it prints x=7. Reason behind it is At compile time what i have disscused above will happen,But at run time jvm looks inside try first there is no exception so jvm will not enter in catch block so it will print x=7;
i think like that thanks Prashant Kumar Singh [ November 06, 2006: Message edited by: Prashant kumar Singh ]