Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initialization of static "constant" field with a non-static value

 
Francesc Martinez
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to initialize a static field but I can't get its value in a "static" way. I mean, I can't do:



So, I thought I could do this:



But this way I have a problem with thread-safety and initialization, so I thought I could do this:



Can you help me with this? I'm in the right way? Are there better ways to do it? Is it completely insane?
I suppose this is a common problem so there should be a "best practice" for it.

If you want a more realistic example, I can tell you why I want to do this: I want to create a static "constant" object that needs a value from a HttpServletRequest (I'm working with a Struts-based framework). Simplifying:


 
Campbell Ritchie
Sheriff
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Setting a final static value from a constructor? So you instantiate one object, and set the value. Then you instantiate a second object, try to reset a final value, and hope the compiler won't notice . . .
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is there a problem with thread-safety? If it's static, then it should be initialized only once, not multiple times.
 
Francesc Martinez
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Setting a final static value from a constructor? So you instantiate one object, and set the value. Then you instantiate a second object, try to reset a final value, and hope the compiler won't notice . . .


I don't understand what you mean. It's not possible to initialize a final static field from a constructor. You have to initialize it inline or from a static block like:

 
Francesc Martinez
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Why is there a problem with thread-safety? If it's static, then it should be initialized only once, not multiple times.


The field is initialized from a thread, so two threads might try to initialize it.


I made a mistake in my first post. I forgot to put the "static" keyword in methods. I'm going to fix it now.
 
Francesc Martinez
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any other idea? Thank you
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your version with the synchronized static set/get methods is probably the best you can do, given your goal.

But maybe we can take a step back: why do you need to do this, exactly? And given that this is in a web application, how can you ensure that no code will try to initialize it a second time? And why is it useful to have an HttpServletRequest live through the whole lifetime of that class without changing -- surely the class will live longer than a single request?
 
Francesc Martinez
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:And why is it useful to have an HttpServletRequest live through the whole lifetime of that class without changing -- surely the class will live longer than a single request?


I don't need the request all the time; I just need it to retrieve some "constant" values that it holds.

But what you said makes me realize that those values are not really constant, so I should retrieve them each time. So, my static field is useless in this case.

Anyway, I suppose my solution is valid for other scenarios.

Thank you very much
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic