🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #18297

getting around the absence of destructors in Java

#18297
From: Bruno Moncorg�
Date: Fri, 17 Jul 1998 00:00
74 lines
2375 bytes
I have a problem finding how to get around the limitation imposed by the
absence of destructors in Java.

The destructor in C++ was very useful to control not just deallocation
(which is done automatically by the garbage collector in Java) but alse
to restore a previous state when the state of something changes
temporarily within a limited scope, the scope of a method for instance.

Let me exemplify this :

Suppose that I want to change the cursor of a frame during a certain
operation and restore it to its previous state when I leave the method.

What I will do in Java is :

method() {
  Cursor oldCursor = getCursor();
  setCursor(new Cursor(Cursor.WAIT_CURSOR));

  // treatment ...

  // everytime I come across a return in my algorithm, just before the
return statement :
  setCursor(oldCursor);

  //...
}

Since this is rather boring, in C++ what I used to do in such a case was
to create an object, say "CursorContext", with an implementation
resembling this :

class CursorContext
{
private:
  Cursor oldCursor;
  Frame f;
public:
  CursorContext(Frame f, Cursor newCursor)
  {
    oldCursor = f.getCursor();
    f.setCursor(newCursor);
    this.f = f;
  }

  ~CursorContext()
  {
     f.setCursor(oldCursor);
  }

};


Then code in my method becomes (assuming I have created a global object
called waitCursor for example) :


MyFrame :: method(){
  CursorContext cursorContext(this, waitCursor);

  // treatment ...

  // when I come across a return statement, nothing to do, since
destructor of local object is called, restoring old cursor
}

Is there any way do to something approaching in Java ?

Thanks for any tips.
Bruno

--
P.S. Please remove nospam. prefix to reply to sender. Thanks
Veillez � supprimer le pr�fixe nospam. pour me r�pondre. Merci
Technical Details
Message-ID: <35AF1714.9C67F8A5@worldnet.fr>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: f74b3,d2f62f86de91d5fb,start
X-Google-Attributes: gidf74b3,public
From: "Bruno Moncorg�" <nospam.moncorge@worldnet.fr>
Subject: getting around the absence of destructors in Java
Date: 1998/07/17
Message-ID: <35AF1714.9C67F8A5@worldnet.fr>
X-Deja-AN: 372299573
Content-Transfer-Encoding: 8bit
Organization: Another Netscape Collabra Server User
Content-Type: text/plain; charset=iso-8859-1
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language