Destructor (Don't laugh!!)
I know that this topic has been beat up many times before, but just hang in there and read the rest of this post before deciding.
I would like the following added to Java: when the last reference to an object goes out-of-scope, a function gets called (usually called a destructor). However, the memory for this object doesn't have to be cleaned up at this time; that's the garbage collector's job. This function is where you can, for example, put code to close files if you opened files in the constructor. Take a look at the following piece of code:
for (int i = 0; i < 10; i++)
SomeClass x = new SomeClass ();
x.addLine ("line " + i);
If you didn't write SomeClass, you won't know whether it opens a file, or outputs to a socket, or handles data in a different way, and you SHOULDN'T have to know that, that's what Object Oriented Programming is all about. However, the writer of the class would like to know when an object of that class goes out-of-scope so that if a file was opened, it can be closed in the "destructor".
I've read posts that say that a destructor is difficult in a language that is based on garbage-collection. However, this would just be a function call. The JVM must know when the last reference to an object is removed so it can queue up the object for garbage collection. At that moment when the last reference is removed, the destructor function can get called.