-
Notifications
You must be signed in to change notification settings - Fork 3
Object methods
##About Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.
But what emthods? Let's try to understand:
Methods:
Object
in Java has:
.toString()
.clone()
.hashCode()
.wait()
.notify()
.notifyAll()
.equals()
.finalize()
.getClass()
But what each method do? And why these so important?
Ofcourse, we want to have string representation of our object, that's why we have toString() in every object.
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
As we can see by default string representation has name of the class and it's hashCode in hexademical representation ,separated by "@". If it's necessary - we can override this method as we want. For example if we have class Person with name and age fields - we can write toString which return String with name and age. When we try to print our class - we use toString too.
In JavaDoc it's recommended that all subclasses override this method
When finalize is called?
As we can see from docs:
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
And after this some of developers think that we can use it for cleaning data. For example, closing resources and smth like that. But it's wrong.
First of all - we havn't got any guarentees that this method will be called. For example, we have forgotten refeferance for our object. And in some cases this method will not be called at all! If our application stops, for example.
And another wrong thought - that if the finalize method lock GC thread - Garbage Collector can't work. It's wrong.
First of all:
The GC doesn't call finalize()
method directly, GC adds corresponding objects to the list, and calls static method java.lang.ref.Finalizer.register(Object). Finalizer class object is a reference to the object for which it is necessary to call finalize (), and stores a reference to the next and previous
Finalizer, forming a doubly linked list. The
finalize() call happens in a separate thread «Finalizer» (
java.lang.ref.Finalizer.FinalizerThread), which is created when you run the virtual machine (or rather in the static section Startup Finalizer class).
finalize()method called sequentially in the order in which were added to the list by the garbage collector. Accordingly, if any
finalize ()will hang he will hang stream «Finalizer», but not the garbage collector. This means in particular that the objects do not have a method
finalize()`(have an empty method), will be properly disposed of, but have to be added to the queue until the droop flow «Finalizer», completes the application or run out of memory.
Implementation of this method from openJDK:
protected void finalize() throws Throwable { }
But overriding this method in some cases is good. For example, if we have an object with Weak/Soft References and work with big file. If we havn't memory - we do not mind that gc destroy our object, but we must to close file. And in this case - it's good approach, i think.
And we can use PhantomReference
instead of finalize()!
To sum up: Don't use it whithout really BIG necessity.