*
The moose likes Java in General and the fly likes Initialization of static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Initialization of static "constant" field with a non-static value" Watch "Initialization of static "constant" field with a non-static value" New topic
Author

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

Francesc Martinez
Greenhorn

Joined: Feb 13, 2009
Posts: 24
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

Joined: Oct 13, 2005
Posts: 38513
    
  23
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
Marshal

Joined: Mar 22, 2005
Posts: 41629
    
  55
Why is there a problem with thread-safety? If it's static, then it should be initialized only once, not multiple times.


Ping & DNS - my free Android networking tools app
Francesc Martinez
Greenhorn

Joined: Feb 13, 2009
Posts: 24
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

Joined: Feb 13, 2009
Posts: 24
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

Joined: Feb 13, 2009
Posts: 24
Any other idea? Thank you
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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?


[Jess in Action][AskingGoodQuestions]
Francesc Martinez
Greenhorn

Joined: Feb 13, 2009
Posts: 24
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initialization of static "constant" field with a non-static value