jQuery in Action, 2nd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Compile time constant Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Compile time constant " Watch "Compile time constant " New topic
Author

Compile time constant

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026


why am I getting error in main method when I try to assign a compile time constant to a byte variable(3). I am doing the same at (2) for byte b where I am not getting any error.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I'll take a stab at this (meaning I could be wrong).
If you look at the JLS, at the definition for a compile time constant, you will find:

# Simple names that refer to final variables whose initializers are constant expressions
# Qualified names of the form TypeName . Identifier that refer to final variables whose initializers are constant expressions


nc.j is not one of these types of names. j is a simple name, and if j was static, FinalVarTest.j would be a correct qualified name.

nc.j is accessing j through a reference to an object. It's a qualified name, but not of the form required by JLS.
[ June 17, 2005: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Here I made j static as well as final:


That compiles.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Here I've used j (simple name) and this.j (a qualified name - but not of the type required by JLS)

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
That was the doubt I had for a long time. Thanks for the wonderful explanation.


 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Compile time constant
 
Similar Threads
Implicit cast
Final variable
Not Getting Compilation Error?
Compile time constant
Compile Time Constant