Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Compile time constants

 
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,
How to determine whether a variable is compile time constant...

The following code from K&B doesn't compile...

 
Ranch Hand
Posts: 128
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I noticed that variable b is not initialized and you have also declared it as final, so you have to give it an initial value at the point of initailization.

Hope that helps!
 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello joseph,

actually it is a mistake, now it is correct...
Still the code doesnt compile,unless assignment and declaration is made at same time..
 
Joseph Mokenela
Ranch Hand
Posts: 128
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hareendra,

I think the problem might be that you are declaring the variables as final, if you want to give them values anywhere in the code except on declaration, I would suggest you don't make them final, just and then you can say It should work.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Final Variables
Declaring a variable with the final keyword makes it impossible to reinitialize that variable once it has been initialized with an explicit value (notice we said explicit rather than default). For primitives, this means that once the variable is assigned a value, the value can't be altered. For example, if you assign 10 to the int variable x, then x is going to stay 10, forever.


K&B BOOK PG 57.

So does this mean that without initialization, the final variable in the above source gets the value 0 assigned automatically, since its the default for primitves(here int), and receives the ability to modify it later

A compile-time constant expression is an expression denoting a value of primitive type or a String that is composed using only the following:

Literals of primitive type and literals of type String
Casts to primitive types and casts to type String
The unary operators +, -, ~, and ! (but not ++ or --)
The multiplicative operators *, /, and %
The additive operators + and -
The shift operators <<, >>, and >>>
The relational operators <, <=, >, and >= (but not instanceof)
The equality operators == and !=
The bitwise and logical operators &, ^, and |
The conditional-and operator && and the conditional-or operator ||
The ternary conditional operator ? :
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


JLS 2nd Edition


http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6350757bug in java but solved!
 
Joseph Mokenela
Ranch Hand
Posts: 128
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hareendra,

I think I misunderstood your question a bit the first time, I think in the case of switch trapping, the case must always have a constant value because it bases itself on a certain condition that must not change in the loop. so the the variable in the case must be a constant.
 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Lalaka Jeerasinghe ,

Thank you for the post, it is very informative..

But i am still unable to convince, why the code doesnot compile even if final variable b is initialized
before it is used in switch .

Anyway if we are using a method variable which is uninitialized the compiler error appears,
but the error here is different...

Can you please elaborate this statement "Simple names that refer to final variables whose initializers are constant expressions "
 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello joseph,

Actually here 2 situations may arise:
1) local variable is final and not initialized before using it in switch , as it is method variable it must be initialized,
Compiler says something like this " variable xxx might not have been initialized"
2) local variable is final and initialized before using in switch (i.e declaration and initialization is done in different statements),
Compiler says something like this "constant expression is required"

Now my question is , why there is second type of error if the first situation alone can handle both the cases...



 
Lalaka Jeerasinghe
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hareendra Reddy wrote:Hello Lalaka Jeerasinghe ,

Thank you for the post, it is very informative..

But i am still unable to convince, why the code doesnot compile even if final variable b is initialized
before it is used in switch .

Anyway if we are using a method variable which is uninitialized the compiler error appears,
but the error here is different...

Can you please elaborate this statement "Simple names that refer to final variables whose initializers are constant expressions "



final variables can be declared as a local variable,
For an example the following compiles and prints out the value of a as 10.



but if you try to do this........


You get a compile time error:-> variable not initialized...

 
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
refer this website.. There is an excellent tutorial regarding compile time constants by Mr. Ankit Garg .. You will get all your doubts clear at there....

JForEach
 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ankit,
That tutorial cleared all of my doubts...
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic