aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confusion regarding compile time constants Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confusion regarding compile time constants" Watch "confusion regarding compile time constants" New topic
Author

confusion regarding compile time constants

Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

That is perfectly legal:


But that one is not legal:


I know that "if(compile time constant)" is considered different than "while(compile time constant)" and I know that "if" is used to provide conditional compilation.
But why is the compiler very nervous about final variables?


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Balraj Momi
Ranch Hand

Joined: Jul 23, 2009
Posts: 45



Here compiler is confident enough that value of i will be initialized, so He is happy with it. But in other example Since it will not initialize compiler has no idea what to print on console. Same problem comes, If you try to use it for other purposes without initialization. I remember in C/C++, If we don't initialize a variable, we would get a garbage value after printing. I guess Java compiler don't want to take that chance.

Regards
Balraj


Regards
Balraj Kumar
SCJP 5 95%
SCWCD 82%
Preparing for SCBCD
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

This is not really an issue with compile time constant -- it is an issue with how the if statement is evaluated.

As you already mentioned, the "reachable code" check for "if" statements is turned off to allowed for conditional statements. Hence, since the check is turned off, it doesn't really know that the "if" block is not executed. And if it is executed, you should get an error as the final variable is assigned twice.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Henry Wong wrote: it doesn't really know that the "if" block is not executed.

Henry



The same in the first case, but despite the fact the compiler allows it.
So the flow control executes after final are assigned?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Lukas Smith wrote:
The same in the first case, but despite the fact the compiler allows it.
So the flow control executes after final are assigned?


The Java compiler makes a distinction between whether code is reachable and whether it can complete normally. A monkey wrench is thrown in with the "if" stsatement. I remember looking at it last year, and coming to the conclusion... "getting a headache -- not really worth my time" ...

Anyway, if you are interested, see section 14.21 of the JLS.

Henry
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Balraj Momi wrote:Here compiler is confident enough that value of i will be initialized, so He is happy with it.


So finally its decided, the compiler is a male


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confusion regarding compile time constants
 
Similar Threads
Case statement
compile time constants
Compile time constant
Final variables
Plz explain me the answer of this code........