• 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

static inner class as cache ?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have an object that gets "recycled"; whatever they call it, it gets set to null few seconds after servlet is rendered. This is not really servlet question, but rather plain java.

I need few variables to copy from that object and I have done it like this:

// This is not a singleton, no instance ever made. Only static stuff.


I think I read (JSR spec) saying that the inner class will be invoked only once in a life of VM, so I guess it's members will as well, so I am granted that thiss will happen only once when the class is used for the first time (which is exactly what I need).

What do you think? Any other cool solution? Give me some inspiration please..

Thanks

[ November 23, 2008: Message edited by: petar banicevic ]

[ November 23, 2008: Message edited by: petar banicevic ]
[edit]Add code tags. CR[/edit]
[ November 26, 2008: Message edited by: Campbell Ritchie ]
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

getThemeDisplay().getPathImage()



Which class declares and defines these two methods?
 
petar banicevic
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by James Clark:


Which class declares and defines these two methods?



My question was rather whether you agree with the "pattern"? (Although this is not pattern, just some lazy Singleton). I am inspired by lazy initialization. Having deep copy of ThemeDisplay is expensive to keep in memory, and lazy init will happen at the right time (when I call it).

The problem could be here:
private static String imagePath=getThemeDisplay().getPathImage();

or I need to change it to:

private static String getImagePath() {
return getThemeDisplay().getPathImage();
}

So lazy is a way to go, right? If so, what do you think about the cache? Is "private static String imagePath" initialized at the outer static class "loading" time or when I reach for "private static String imagePath"?

(Regarding classes: I think their declaration isn't important. It's part of Liferay portal.)

[ November 26, 2008: Message edited by: petar banicevic ]

[ November 26, 2008: Message edited by: petar banicevic ]

[ November 26, 2008: Message edited by: petar banicevic ]
[ November 26, 2008: Message edited by: petar banicevic ]
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

private static String getImagePath() {
return getThemeDisplay().getPathImage();
}



What class defines methods getThemeDisplay() and getImagePath()? Is this the same class that will also contain a static reference to the "cache"?
 
petar banicevic
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by James Clark:


What class defines methods getThemeDisplay() and getImagePath()? Is this the same class that will also contain a static reference to the "cache"?



No. getThemeDisplay, getImagePath are member methods in some other class (singleton) that I don't know much about. That "unknown" class won't touch my cache.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this topic is no longer a simple beginner's topic. Moving.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

petar:
I think I read (JSR spec) saying that the inner class will be invoked only once in a life of VM



This statement is a little ambiguous. What do you term as "invoked only once"? I can have a static inner class and invoke (call a method) at will any number of times.
As a matter of fact any static initialization in a class is done once. So, your code:

if put directly under the class PortalHelper will also serve "nearly" the same purpose as the present case. "nearly" because it will execute only once and as soon as PortalHelper is invoked first. I guess you want to have "lazy" initialization so what you are doing makes sense.
Infact this is as suggested by Brian Goetz in this article and is called as "Initialize-on-demand Holder Class idiom"

petar: Is "private static String imagePath" initialized at the outer static class "loading" time or when I reach for "private static String imagePath"?


It will be invoked when the class Cache gets initialized i.e. any method/static member variable is accessed.
 
petar banicevic
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ha ! Thanks. This is what I wanted to hear. Now, if you alow me, please, 2 more questions related to the same example from top of the thread.

When is the line:

private static String imagePath=getThemeDisplay().getPathImage();

invoked exactly:

1) When someone invokes static method from PortalHelper class or
2) when someone invokes static method from the Cache class

I really count and hope that 2nd is correct answer. This way I get "on demand init" of *inner* class, which is something new for me and I need confirmation.

Disclaimer - If you think I am gone crazy with this "inner" stuff, just to defend myself a bit: I need *one* static utility PortalHelper with more complex functionality inside. Inner class would give me more freedom at design then one single static method.

Also I need to be sure that above example is *not* equivalent to following modification :



If it is, then answer (1) would be correct, which won't support idea about lazy init of inner class....

Thanks a lot for reading this looong mail....
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add my thoughts on the problem...

Originally posted by petar:

When is the line:

private static String imagePath=getThemeDisplay().getPathImage();

invoked exactly:

1) When someone invokes static method from PortalHelper class or
2) when someone invokes static method from the Cache class



The answer is 2)

If it is, then answer (1) would be correct, which won't support idea about lazy init of inner class....



What i understand by your OP is that you don't want
private static String imagePath=getThemeDisplay().getPathImage(); to be initialized at the time of loading of your PortalHelper class.

Just declaring and not initializing the static class *Cache* reference variable in the enclosing class *PortalHelper* would not run the static member and block initialization for the class *Cache* on instantiation of class *PortalHelper*.

So, if the purpose of your static inner class is to shield the code that you want to be executed only on demand then i guess you are on the right track.
The article link given by Nitesh corroborates that.
 
petar banicevic
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your effort, you understood situation correctly. "Shield" is what I wanted to do.

Huh, I am reliefed now. Many thanks.

BTW, yes, the article confirms that as well, it's a good one:

"(...), and its lazy initialization from the fact that the inner class is not loaded until some thread references one of its fields or methods. "
[ December 05, 2008: Message edited by: petar banicevic ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic