T
The Ghost In The Machine
In comp.lang.java.advocacy, Remon van Vliet
<[email protected]>
wrote
http://www.hackcraft.net/raii/
"Resource Acquisition Is Initialisation"
...
[begin excerpt]
Which Languages are Appropriate for RAII
Or rather, which languages is RAII appropriate for. As will soon be
explained RAII depends on the way that constructors and, in
particular, destructors, get called and the predictability of this
happening.
RAII can be used with:
* Languages which can have user-defined types allocated on the
stack (automatic objects in C/C++ terminology) and cleaned up
during normal stack cleanup (whether because of a function
returning, or an exception being thrown). E.g. C++.
* Languages which have reference-counted garbage collection
and hence predictable cleanup of an object for which there
is only one reference. E.g. VB6
RAII cannot generally be used with languages that clean up
objects using an unpredictable garbage collection, such as
Java (however see the Postscript on .NET). If a language
guarantees cleanup of all objects before an application
shuts down then it may be applicable to some problems.
[end excerpt]
I am not certain whether an Object variable going out of scope
in Java is subject to automatic cleanup or not, and if it is,
under what circumstances. Obviously it cannot be cleaned up
if the Object is placed into a Map or Collection. Otherwise,
I don't know.
In Java one can use a try{}catch{}finally{} pattern to
emulate RAII, but it has to be explicitly programmed.
One can also attempt overload of the finalize() method,
but the problem is that one has no idea exactly when
that will be called -- if at all. Some patterns (Swing)
implement a dispose() method as well, which Java does *not*
support directly.
<[email protected]>
wrote
Martin Vejnár said:The said:[5] Java doesn't have explicit deletes. The garbage
collection is expected to take care of things. (There are
some exceptions; a FileOutputStream for example will be
left open indefinitely, even if the reference is lost.)
For some reason, you've put the most important statement in parentheses.
RAII is one of the two reasons I stick with C++. I don't know of any other
language that would support such concept. (C# and D both support RAII, but
require the programmer to explicitly mark objects that should be destroyed
when leaving scope. Why?) How do you do RAII in Java?
Have you ever used Java and actually ran into an issue that requires RAII?
http://www.hackcraft.net/raii/
"Resource Acquisition Is Initialisation"
...
[begin excerpt]
Which Languages are Appropriate for RAII
Or rather, which languages is RAII appropriate for. As will soon be
explained RAII depends on the way that constructors and, in
particular, destructors, get called and the predictability of this
happening.
RAII can be used with:
* Languages which can have user-defined types allocated on the
stack (automatic objects in C/C++ terminology) and cleaned up
during normal stack cleanup (whether because of a function
returning, or an exception being thrown). E.g. C++.
* Languages which have reference-counted garbage collection
and hence predictable cleanup of an object for which there
is only one reference. E.g. VB6
RAII cannot generally be used with languages that clean up
objects using an unpredictable garbage collection, such as
Java (however see the Postscript on .NET). If a language
guarantees cleanup of all objects before an application
shuts down then it may be applicable to some problems.
[end excerpt]
I am not certain whether an Object variable going out of scope
in Java is subject to automatic cleanup or not, and if it is,
under what circumstances. Obviously it cannot be cleaned up
if the Object is placed into a Map or Collection. Otherwise,
I don't know.
In Java one can use a try{}catch{}finally{} pattern to
emulate RAII, but it has to be explicitly programmed.
One can also attempt overload of the finalize() method,
but the problem is that one has no idea exactly when
that will be called -- if at all. Some patterns (Swing)
implement a dispose() method as well, which Java does *not*
support directly.