• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Query on wrapper constants

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Can anyone pls tell me why wrapper variables are not treated as
constants even those declared as static final. They are not allowed to e part of case expr in switch.

public static final Integer x =9;
 
author
Posts: 23951
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
The switch/case statement uses primative ints -- and autoboxing is only integrated with it. Maybe in the future, the case statement will be extended to understand that it is a constant being autoboxed, but not currently.


Hmmm... Thinking about this a bit more. Autoboxing would require that the intValue() method be called. I don't think the specification would ever allow a method to be called and still be considered a constant. (I don't know. Just thinking out loud here)

Henry
[ December 23, 2007: Message edited by: Henry Wong ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think you know about impact of final on object.
once you declare and initialises with object you cannot make the variable to point to another object.but you are allowed to change the value of the object.think of this code
publi static final Integer ABC=new Integer(4);
ABC=new Integer(2);
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, i tried to code like this --

When you say new Integer(3), you are essentially creating a new object and assigning its address to the reference variable i (which is final). So it gives compiler error!
[ December 24, 2007: Message edited by: prajal Mehta ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Henry]: Hmmm... Thinking about this a bit more. Autoboxing would require that the intValue() method be called. I don't think the specification would ever allow a method to be called and still be considered a constant. (I don't know. Just thinking out loud here)

That's correct, at least as far as what the current spec says. It's possible to imagine that they could, if they wanted to, add some special rules to incorporate Number wrappers as part of compile-time constant expressions. But I don't think there's really much need for this in any event. Any time you've got an Integer whose value is known at compile time, you could easily replace it with an int, after all. I suspect that the number of times people have had any real problems because of this is very small indeed. I doubt it's worth spending the time to rewrite those rules and modify the compiler accordingly.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic