File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes static inner class as cache ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "static inner class as cache ?" Watch "static inner class as cache ?" New topic
Author

static inner class as cache ?

petar banicevic
Greenhorn

Joined: Jul 21, 2008
Posts: 9
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 ]
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
getThemeDisplay().getPathImage()


Which class declares and defines these two methods?
petar banicevic
Greenhorn

Joined: Jul 21, 2008
Posts: 9
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

Joined: Apr 16, 2008
Posts: 2187
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

Joined: Jul 21, 2008
Posts: 9
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
I think this topic is no longer a simple beginner's topic. Moving.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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.


apigee, a better way to API!
petar banicevic
Greenhorn

Joined: Jul 21, 2008
Posts: 9
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....
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
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.


thanks
Harvinder
petar banicevic
Greenhorn

Joined: Jul 21, 2008
Posts: 9
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 ]
 
Don't get me started about those stupid light bulbs.
 
subject: static inner class as cache ?
 
Similar Threads
Doubt on Inner class
Method_Inner_class
Question about "Getting in Touch with your Inner Class "
Dealing with Jlist
sorting table columns