Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Java compile time constants confusion

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

https://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
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic