Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Destructors

Status
Not open for further replies.

fatcodeguy

Programmer
Feb 25, 2002
281
0
0
CA
How do I set up destructors in Java. When I exit the program, or end a connection, I want it to display that the connection has been closed. I know how to do this in C++, but not in Java. Any help would be great! Thanks
 
Java does not have destructors. However, you can try two things.

1. Have thread that has references to all of your connections and calls Connection.isClosed() at regular intervals. If it detects that a connection is closed, it can output a message, etc.

2. Extend the Connection class. In the new class, overload the close() method with something like:
Code:
super.close();
System.out.println("Connection closed.");
Connections are closed automatically in java via garbage collection and the api says to use the close() if you want to force the closure. Since I do not know if the garbage collecter calls the Connection.close() method, I do not know if this will work for connections that are not explicitly closed in the code.
[morning] HavaTheJut
 
Hi ,

Enclose your part of the program or method that makes the connection in to a try-catch block and also include the finally block.
If any problem occurs in connection then it would be handled in catch.The statements in finally will execute immaterial of whats done in try or catch.
So do all you clean-up(closing of resources) in the finally block.This is exactly not like a destructor,but you can
do all the clean up at a single point.

regards,
VGG
 
The finally block is good for ensuring that something happens in your program, as the contained code always executes. However, if you open a connection in the try-catch-finally block and do not explicity close it (i.e. if you want to pass the connection to another method) the connection will (probably) not be closed when the finally block executes. java.sql.Connection is automatically closed by the garbage collector when it is no longer referenced. [morning] HavaTheJut
 
Just because you create Connection in the try block does not mean you can't close it in finally... and yes it does mean that it is closed.
Example:
Code:
Connection conn = null;
try {
  conn = getConnection();  // get you connection somehow
  // Do stuff with conn
}
finally {
  if (conn != null) { 
    try { conn.close(); }
    catch (Exception ex) {}
  }
}
System.out.println("Connection is closed.");

If the code gets to the print statement then the connection will be closed. What you can't guarantee is that something else didn't already close the connection, hence we check to see if it is null already before closing it. I don't understand why you have the impression that closing connections in a finally block doesn't work.

Back to the question at hand. Java does have constructs similar to destructors, it is the finalize() method. finalize() is guaranteed to be called by the JVM before an object is garbage collected. It is more better to be proactive about things like connections though and release them as soon as one is finished with them.
 
Hi,

Regarding finalize method,
be a bit careful when using it in multi-threaded code.I am giving you the description given in the java1.2 doc

" The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates. "

regards,
VGG
 
Good addition vgg. I just mentioned finalize() because alot of people don't even realize it exists. Definitely it much better to write code which does not need a finalize(). Even in C/C++, destructors were normally just used to reclaim memory used by the object, resources were usually (at least by good developers) managed more judiciously. Proactive is always the way to go.

Sorry about the typo with "more better", I sound like an idiot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top