Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,979
    Thanks
    120
    Thanked 76 Times in 76 Posts

    destruct inside construct

    since I call this from many pages, not knowing if
    session object called "sessionObject" already exists, I do each time:

    Code:
    SessionObject sessionObject= new SessionObject(request);
    Code:
    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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,495 Times in 4,459 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •