PDA

View Full Version : destruct inside construct


BubikolRamios
01-04-2010, 01:39 AM
since I call this from many pages, not knowing if
session object called "sessionObject" already exists, I do each time:


SessionObject sessionObject= new SessionObject(request);




package mc.session.objects;

public class sessionObject
{
....
public sessionObject(HttpServletRequest req) throws Throwable
{
HttpSession session = req.getSession(true);
if (session.getAttribute("sessionObject") != null)
{
// destruct this object as one instance of it, and only one
// that I need is already created
super.finalize();
}
}



Anything that I don't see here ?

Hope this "sessionObject" gets collected by gc
at the time session ends.

Old Pedant
01-04-2010, 08:35 PM
I wouldn't do it this way, at all.

I'd use a singleton. That is, a class where only one instance of it *can* be created.

Look up "singleton pattern" in the docs.