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

Constants and static final Wrapper Objects

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we use a static final wrapper object as a constant in java? If not, reasons Please? Thanks in Advanced!
 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i surely want to know the answer for that....because i think you cant use them as a constant....but want to know why....
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yaaa.... It's true, we can't use it. But don't know the reason
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my reasoning to this is..., when the compiler unwraps the wrapper object, what we get is only a mere value held by that wrapper object of particular type and it might not be coming out with its "static" and "final" flags turned on. and since compiler seeks constant expressions, it flags error.

this is what i think, i'm also a learner.
 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:Can we use a static final wrapper object as a constant in java?



Abimaran,

When you say "use... as a constant" whcih statements are you talking about?

Thanks,
Nidhi
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well I agree with Rushikesh.
Wrappers are still objects refering to some primitive values and the modifiers static and final are for the wrapper object not the primitive.
So as rushikesh said when the primitive is unwrapped ther is no static or final modifier attached to it.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did i really clear scjp? how did i clear it? i know nothing
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So we cannot use it in Interfaces? Please confirm?
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:So we cannot use it in Interfaces? Please confirm?


you mean declare?
we can declare wrapper objects in interfaces but can use them as constants in the classes that implement those intefaces.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yaa, but declare and initialize it within an Interface.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you explain with an example. i can declare like this , static final Integer i=20;
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use as constants means:

if you try to use a wrapper object in a switch loop as one of the case value, it would fail and the reason is
Because autounboxing converts

case int1:



into

case int1.intValue():



which is not a compile-time constant.

http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.8

also, one more thing
The reason it is not allowed is ,we can assign a null value (it is not necessary to assign a int value when we declare final variable) to the integer wrapper class and in such a case it will throw NumberFormatException if used in the case statement.

So it is designed in such a way to avoid this problem by not allowing wrapper class in case statement.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic