• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java compile time constants confusion

 
Jeremy Hayes
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21005
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21005
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic