• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

final wrapper class object

 
Ranch Hand
Posts: 34
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source http://www.etattva.com/


the above code doesnt compile.The explaination given is:
case expression must be a constant expression. Final objects are not allowed in a case statement. A final object?s value can be changed whereas a final variable's value cannot be changed.
My question is how we can change the value of objects of wrapper classes which are final?
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In case of final variables to primitive types, you can not change its value, because, in memory, the value of that variable is in the same memory slot that the variable itself, but in case of final variables that refer to objects, the only thing is final is the reference to the object, I mean, in an variable that refer to an object the only thing that is on that variable is the memory address to the object, so, you can not make:


The object itself is on other memory place (called the "heap") and it's not final so, lets say your class A has a public int attribute called aa that is not final, like:


So it is allowed to change the aa's value, like:


As I know, you can not change the value inside a wrapper class, but the compiler does not see the wrapper class, it just see it is an object and that is not allowed in a switch-case statement.

Hope this is clear, regards,
[ December 29, 2008: Message edited by: Alejandro Galvan ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.. i think the issue is with what compile time constants really are. A Wrapper object , even when referenced by a final reference variable, doesn't seem to be regarded as a compile time constant.

I saw the same question answered Here
 
Shwetha Sharma
Ranch Hand
Posts: 34
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Alejandro for the quick response but i was confused with the final objects of wrapper classes.

Like here we have
final Integer a = 1;
now according to me we cannot change the value of the object a (neither can we change the refrence as it is marked final)
so here why it is not taken as a compile time constant and use it in case statement.
My question is can final objects of wrapper classes change their value ??
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sharma,

The answer is no (a final wrapper reference will always have the same value.)
There is no reason why it couldn't be used as a compile time constant if the compiler can determine its value at compile time. It's just that the specification at this point doesn't consider them to be "compile time constants" (the specification defines what a compile time constant is, and it is free to make any decisions to that regard that may seem arbitrary.) Also, the specification (like Henry pointed in the other post) might change.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ruben is right here, java considers only final primitives and Strings as compile time constant.

And as you know case statements allow only primitives upto int(byte, char ,short), so you cannot use any reference type in case statement. If you make an Integer final, than you are writing as reference type in case, so its not allowed.

Another case where compile time constant needed is in inner class where only static fields allowed that are compile time constant.

for ex:


class Outer{

class InnerClass{

final static int constInt=5;
final static String constStr="abc";
final static byte b=5;
final static long l=5;
final static float f=5;
final static double d=5;
final static Integer i=5;//error
static int i=5;//error
final static String s;//error
s="";//error



}
}

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Punit,
Thanks for the explanation and the fact regarding the inner class. "Another case where compile time constant needed is in inner class where only static fields allowed that are compile time constant."

The above is not mentioned in the K&B book (or I overlooked it ?) . Could you please let me know the source of it.

Thanks in advance,
Aruna.
 
It will give me the powers of the gods. Not bad for a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic