Re: Class destructors




"mlw" <mlw@xxxxxxxxxxxxxx> wrote in message
news:duGdnaEi0J4EvLXbnZ2dnUVZ_q_inZ2d@xxxxxxxxxxxxxx
~kurt wrote:

mlw <mlw@xxxxxxxxxxxxxx> wrote:
Could someone explain why there is no destructor in Java classes?


The explanation I remember is destructors are generally used to free
memory, and Java has garbage collection. Doesn't make it right, but that
is the way it is.

That's one of the things that bothers me about Java. So many newbe CompSci
people think it is wonderful, but it has so many glaring omissions and all
too often it falls short of a real and useful programing language. Like
Windows, skill in Java comes from knowing the trivia of Java, and not from
the theory and science of your algorithms.


Total nonsense. Destructors have nothing whatever to do with general theory
and science of algorithms.

In the C++ object model where objects (class instances) and the lexical
reference to them have a one-to-one relationship destructors make sense. The
fact that C++ does not offer GC makes them even essential. And yes, many
other usefull constructs can be expressed using destructors.

In Java (and most other contemporary programming/scripting languages) the
object instance and the syntactical construct referencing it are loosely
coupled. Objects can have zero or more references pointing to them, not
unlike C++ pointers (or C++ references, for that matter). Such an object
model does not allow the definition of a destructor like construct because
the lexical scope of an object reference and the lifetime of the actual
object instance are at best indirectly related.

If you value a theoretical approach to programming than you should be able
to distinguish abstract algorithms from language constructs that allow you
to implement them. If you find yourself unable to implement your abstract
algorithm in a language without destructors (which would not leave you many
options anyway) then you obviously are unable to make that distinction.

Silvio Bierman


.



Relevant Pages

  • Re: finalize() not guaranteed to be called -- ever
    ... >> problem is that objects in variables have value semantics and not ... is also the justification for not including this possibility in Java. ... Just adding destructors ... You suggest that the JVM can just guarantee that finalize() is called, ...
    (comp.lang.java.programmer)
  • Re: CLOS and C++
    ... > time with the lack of destructors. ... as part of 2 you can point out that Java has no destructors. ... the new version of finalization that actually work. ... the Lispy equivalents are; certainly they are implementation ...
    (comp.lang.lisp)
  • Re: finalize() not guaranteed to be called -- ever
    ... Then the Java object should have some kind of dispose method. ... It is very different in that it is possible to write C++ programs in which the destructors are never invoked manually but do run. ... C++ you can put objects on the heap as well and when you do the destructor is called manually using the delete statement. ... Either the object itself is stack based or you use a stack based object to invoke the ...
    (comp.lang.java.programmer)
  • Re: Destructors are useless?
    ... On process exit, Java ... >that destructors (or finalizers, if you prefer) will *not* run. ... >assertions are checked whenever the GC decides to garbage collect an object. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Destructors are useless?
    ... > It is interesting that Java doesn't have this problem. ... > that destructors (or finalizers, if you prefer) will *not* run. ... > safe to put assertions into Java finalizers: ...
    (microsoft.public.dotnet.languages.csharp)