What is the function of finalize in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the function of finalize in java?

as per my knowledge Finalize () in java is used for end up codes before the object is removed from heap . System.gc i.e garbage collector is responsible for deallocation of memory . If m wrong please correct me up

18th Jul 2017, 5:23 AM
Samrat
Samrat - avatar
4 Answers
+ 2
What they mean is Garbage collection is performed by a daemon thread called Garbage Collector(GC). This thread calls the finalize() method before object is garbage collected.
18th Jul 2017, 5:33 AM
Apoorva Shenoy Nayak
Apoorva Shenoy Nayak - avatar
+ 1
finalize() acts similar to a destructor for a java class. finalize() does NOT call the garbage collector. In fact it is the other way around. The garbage collector calls the finalize() method of a class just before the object is about to be garbage collected. This can happen any time after the object becomes eligible for GC. An object becomes eligible only when there are no more references to that object. If you were to explicitly call the finalize() method of an object it will still be called again by the GC when eligible and just before collection. The finalize() method is where you should do any necessary clean up, such as freeing up any held resources, closing a connection, etc.
18th Jul 2017, 6:00 AM
ChaoticDawg
ChaoticDawg - avatar
0
Yes, Absolutely correct!
18th Jul 2017, 5:28 AM
Apoorva Shenoy Nayak
Apoorva Shenoy Nayak - avatar
0
but in Java challenge here in solo learn they are keeping ans as finalize is used to deallocation memory
18th Jul 2017, 5:32 AM
Samrat
Samrat - avatar