File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Java compile time constants confusion 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 » Beginning Java
Bookmark "Java compile time constants confusion" Watch "Java compile time constants confusion" New topic
Author

Java compile time constants confusion

Jeremy Hayes
Greenhorn

Joined: Sep 10, 2009
Posts: 2
Firstly, I'd like to thank Henry Wong for his piece on this subject ( see the forum FAQ if you haven't seen it ).
I still have a few issues with this subject though. I should explain that there seems to be some incorrect or out of date info on Java constants on the web, which increases the confusion level.

So here are my questions/assumptions:

1. Do "Load time constants" exist?
I have seen some suggestions that they do, but the examples given fail to compile.
For example the code:

I've also read that emuns are load time constants that are somehow fudged to
work as switch case expressions by the compiler.

So my assumption is that there are only compile time constants. Am I correct?

2. The definition of the constant expression in the JLS makes no distinction between instance constants or class constants ( or any scope ).

Can I then assume that there are no differences or exceptions based on where the constant is defined?

3 As a follow up to point 2, why can't I use a final method parameter in a switch statement?

an example:

The constant 'one' above cannot be used in the switch, so:
a. Is it a constant?
b. What part of the JLS definition of the ConstantExpression does it break?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

Firstly, I'd like to thank Henry Wong for his piece on this subject ( see the forum FAQ if you haven't seen it ).


You welcome... ... Here is a link for those interested...

http://www.coderanch.com/t/454384/Beginning-Java/java/What-compile-time-constant


2. The definition of the constant expression in the JLS makes no distinction between instance constants or class constants ( or any scope ).

Can I then assume that there are no differences or exceptions based on where the constant is defined?



The JLS makes no distinction on the type of constant variable. So, there is really no different between a class or instance constant variable -- as long as it is in scope, it can be used as a constant.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

3 As a follow up to point 2, why can't I use a final method parameter in a switch statement?

The constant 'one' above cannot be used in the switch, so:
a. Is it a constant?


"final" is not the same thing as "constant". The "one" parameter is not a compile time constant.


b. What part of the JLS definition of the ConstantExpression does it break


A variable must be assigned to a compile time constant expression for it to be considered as compile time constant variable (among other things). Parameters are evaluated when the method is called, which is evaluated at runtime. Hence, not a compile time constant expression.

Henry
 
Consider Paul's rocket mass heater.
 
subject: Java compile time constants confusion