aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Local variable initialization-Its magic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Local variable initialization-Its magic" Watch "Local variable initialization-Its magic" New topic
Author

Local variable initialization-Its magic

Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Why this

fails the compilation?
And this

passes the compilation?
Please help!
[ November 06, 2006: Message edited by: Sanjeev Kumar Singh ]

~Sanjeev Singh<br />SCJP 1.5
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Hi Sanjeev,

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!
Saurabh Vyas
Ranch Hand

Joined: Sep 02, 2003
Posts: 72
Hi Ranchers,

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
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Hi Above,

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...
Prashant kumar Singh
Greenhorn

Joined: Nov 04, 2006
Posts: 22
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
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
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.

Ranchers correct me if I am wrong.
Jae Stryker
Greenhorn

Joined: Oct 31, 2006
Posts: 21
Originally posted by Sanjeev Kumar Singh:
Why this

fails the compilation?
And this

passes the compilation?
Please help!

[ November 06, 2006: Message edited by: Sanjeev Kumar Singh ]



if (for whatever reason) the try block fails how can y = x when x has not been initialize yet, it can't.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Hoorah to Rachers!! We got it.
Prashant kumar Singh
Greenhorn

Joined: Nov 04, 2006
Posts: 22
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 ]
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

why are some of you are talking about scope always?

The question has nothing to do with scope.
Variable x is declared in the main method, so it is visible there. Also in all block inside.

The question is if x gets initialized in all bifurcations of the code. Wouldn't be necessary if x wasn't used, though.

So initializing it in a try block alone won't do, must be as well in the catch (or finally) block.
Same would be true for if/else.


Yours,
Bu.


all events occur in real time
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Thans Bu.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Local variable initialization-Its magic