wood burning stoves 2.0*
The moose likes Java in General and the fly likes The strange ways of compile time constants 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 » Java » Java in General
Bookmark "The strange ways of compile time constants" Watch "The strange ways of compile time constants" New topic
Author

The strange ways of compile time constants

Steli Niculescu
Greenhorn

Joined: Jul 14, 2009
Posts: 14
This, obviously, does not compile.


This compiles:


This, not so obviously, does not compile :


This DOES COMPILE :


Why ??!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575



there is a possibility to b can be false, so local variable x could not be initialize before it get use.


compiler make sure that x will get initialize here. so no error.


here after the exception, code cannot be reached.hence error.


[I am not sure]I think this is because of java compiler(javac) dont have pre-processor
Steli Niculescu
Greenhorn

Joined: Jul 14, 2009
Posts: 14
Yes, I'm not sure either, actually my question was only for the last code sample.
I thought the compiler is sure that the exception will be thrown and the last statement will be unreachable, but it seems that's not true.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
It may be easy for a human being to spot that the last two code examples are identical, but it's not so easy for a compiler. You could make its analysis of which code will definitely be executed (or which won't) as complex as you want, and it will still miss opportunities for proving that some code can't be reached. It has to draw the line somewhere, and that line seems to be trying to prove the constant-ness of "if" condition values.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

The compiler allows this to build code like this:

It would be very annoying to get unreachable statement errors if Debug.enabled is set to false.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The strange ways of compile time constants