Deterministic destruction of Objects in Java - An idea
I love programming in C++, despite the hues and cries against it as a "most programmer unfriendly" language. I have my own reasons to like C++. Top two reasons being:
This guarantee is a very strong weapon for any programmer. It makes destruction of an object a deterministic phenomena. Objects created in heap using new key word require the user to explicitly destroy them, and thats not what I am discussing here.
One of the good programming practice is to acquire and release any resource within the same lexical scope, as much as possible. (I hear you yelling "Its not always possible, fella" and I agree with you!)
If we look at the object life cycle models provided by C++ and Java, C++ implicitly supports the above mentioned practice. But in Java, every user of the object must remember to invoke the destructor's equivalent (freeResources, resetAll, etc). finalize method in Java is altogether for a different purpose, and the language inventors themselves strongly discourage using and relying upon finalize method.
I was chewing this idea for sometime and this is what I feel.
Is there a rationale behind not supporting deterministic object destruction in Java?
- Bjarne was born in 1950, it was the year when my father was born.
- Initial C++ (C With Classes) was released in 1979, it was the year when I was born. :-)
This guarantee is a very strong weapon for any programmer. It makes destruction of an object a deterministic phenomena. Objects created in heap using new key word require the user to explicitly destroy them, and thats not what I am discussing here.
One of the good programming practice is to acquire and release any resource within the same lexical scope, as much as possible. (I hear you yelling "Its not always possible, fella" and I agree with you!)
If we look at the object life cycle models provided by C++ and Java, C++ implicitly supports the above mentioned practice. But in Java, every user of the object must remember to invoke the destructor's equivalent (freeResources, resetAll, etc). finalize method in Java is altogether for a different purpose, and the language inventors themselves strongly discourage using and relying upon finalize method.
I was chewing this idea for sometime and this is what I feel.
Garbage collection and object destruction are two different things. Whenever a lexical scope exit happens, the JVM has the list of objects within that scope. JVM can decide if an object still has any references to it, or it is safe to be destructed. Thus destructed object can wait in heap till GC occurs.My thought is not too deep to be incorporated into a production release! But it certainly is worthy enough to be considered.
Is there a rationale behind not supporting deterministic object destruction in Java?
Comments
-Java has the garbage collection feature jus to ensure no memory leaks occur.
-I think in Java there is way to explicitly call the Garbage collection. So no one waits :)
-Introducing destructors concepts in Java will lead to memory leaks and mismanaged objects. This when creating large enterprise softwares will be a huge overhead.
These are just my ideas. So no hard feelings towards C++. C++ is still capable of doin so many things Java cannot do.
Though GC maynot be the best way to handle "dead" objects, its sure is a good effort to ease the programmer. Maybe a new GC design will be the solution. ;)
Maybe Harris did not get the point because he is a Java native and never has known the usefullness of creating an object through the stack
.NET provides the 'using' keyword that enables you to do that ¿is there anything similar in java?
thanks
Fernando Simoes