Java Methods for Ending Object Life

An experienced programmer new to Java looking for your wisdom:

If there is no way to guarantee that any particular chunk code executes as an object goes out of scope, then what other approaches exist that will offer the same functions? (it seems that finalization is clearly not intended for this)

A classic example is the idiom of locking a lock:

void method() { // Thread-unsafe operations {...} { // <- New scope // Give a mutex to the lock ScopedLock lock(m_mutex); // thread safe operations {...} if (...) return; // Mutex is unlocked automatically on return // thread safe operations {...} } // <- End of scope, Mutex is unlocked automatically // Thread-unsafe operations {...} } 

I can understand that in Java it would be disapproving if some code were executed, if you had not explicitly called it. But I believe that the ability to enforce any code that will be executed at the end of an object’s life is a very powerful feature to ensure that your classes are used reasonably by client code. Thanks

+7
java
source share
4 answers

If you want any code to be run in METHOD anyway, just use try ... finally .... The final block is guaranteed to work. Or, in Java 7, the c block is used.

If you want some code to run as a C ++ destructor. I am afraid there is no such thing in Java. The finalize method is not reliable and cannot be used to handle critical missions. As usual, Java classes usually expose a cleanup method (for example, close () of these stream classes), so the client explicitly calls this method to perform cleanup tasks.

+1
source share

In the general case, if you need to actually close / delete resources, it is recommended to use the try{} finally{} structure.

  // The old way - using try/finally Statement stmt = con.createStatement(); try { ResultSet rs = stmt.executeQuery(query); while (rs.next()) { // ... } } catch (SQLException e) { // Whatever ... . } finally { // Be sure to close the statement. if (stmt != null) { stmt.close(); } } 

Later java incarnations have an AutoCloseable interface that you can use with the with mechanism. All Closeable objects are automatically AutoCloseable .

  // Example of what is called try-with try (Statement stmt = con.createStatement()) { ResultSet rs = stmt.executeQuery(query); while (rs.next()) { // ... } } catch (SQLException e) { // Whatever ... stmt will be closed. } 
+9
source share

As of Java 7, there is automatic resource management. If your lock is under your control, then make it an AutoCloseable implementation. Unfortunately, java.util.concurrent locks do not implement this interface. Here is a good recommendation for details.

+2
source share

Another option for replacing the finalize method is PhantomReference . If you want to perform an action before the object is garbage, it offers another good approach better than the finalize method.

Check here: https://weblogs.java.net/blog/kcpeppe/archive/2011/09/29/mysterious-phantom-reference

+1
source share

All Articles