• Post Reply Bookmark Topic Watch Topic
  • New Topic

finalize() question  RSS feed

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Question:
If we don't code finalize(), will JVM execute the default finalize() method in Object class
Answer:
No..JVM will try to execute the inherited finalize() in the super class, if any.

The above question is from earlier thread, am confused with the answer can any one give more details about it.
My basic confusion is with the statement "in the super class, if any" in the answer, since the Object class is parent of all classes why finalize() method of Objects class doesn't get executed when no finalize() method is overridden in our program. I know that
protected void finalize() throws throwable
is a "do nothing" method, my question is whether it gets called or not in this situation?.
Also I read somewhere that before Garbage collection finalize method WILL get executed. Is this statement true only if finalize method is overriden in our program, if not then only option left is to execute finalize() method of Object class.
Please help me in resolving my confusion.
Thanks in advance
 
Khurram Fakhar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually , Gabage Collector is on a very low priority thread, whose priority is 1.No one can give 100% gaurenttie that garbage collector can collect all the objects from the memory which r no more in use. That is very important and we should be very much clear about it.
now actually GC will make a map to all the references of the objects. and after sometime it will scan program to figure out different objects which r referring to NULL or which r out of scope.
If refence is not accessable in program anywhere else then it will collect its refence and call the finalize method..
If finalize is overriden then GC will allow the overriden code and even then allow that code to be remained in the memory for some time so that to check weather it is needed anywhere in the program or not , After that GC will remove it
i hope this theory will work
if not then i can explain more with code
Regards
Khurram Fakhar
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Khuram
First of all thanks for getting back to me with nice info about gc, however I will appreciate if you could answer strictly to my confusion.
Let me try to simplify wording of my question
Question 1
If finalize method is neither overriden in the my program nor in its super class then ,whether it will call default finalize() of Object class or not (Y/N with brief explanation)
Question 2
Whether GC will always call finalize()method before garbage collection of any object,eligible for GC.
(Y/N with brief explanation,
if ans if Y then what happens when finalize() is not overriden in my program or its super)
Please explian me these two questions, I will really appreciate for your time. I am just trying to learn with the help of you guys.
Thanks
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys
can anyone please help me with these questions???
Thanks
 
Ankur Gupta
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siva,
Hope this helps...
protected void finalize()
throws Throwable
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.
The general contract of finalize is that it is invoked if and when the JavaTM virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output
connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.
The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.
The finalize method is never invoked more than once by a Java virtual machine for any given object.
Any exception thrown by the finalize method causes the iinalization of this object to be halted, but is otherwise ignored.
This was from the Java API. you should always try and have a look at the API and JLS. Writing some code and testing always helps(that goes without saying!!).
Ankur
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets see Siva :
Question 1
If finalize method is neither overriden in the my program nor in its super class then ,whether it will call default finalize() of Object class or not (Y/N with brief explanation)
Yes, it calls the finalize method from Object (all classes inherit from Object)
Question 2
Whether GC will always call finalize()method before garbage collection of any object,eligible for GC.
(Y/N with brief explanation,
if ans if Y then what happens when finalize() is not overriden in my program or its super)
Answers is yes again. When the finalize method is not overriden it calls the filalize method from Object that does nothing.
Note: Overriding the finalize method is like overriding an ordinary method, it is recomended to call super.finalize() at the beginning of your finalize method code if you are inheriting from a clas that performs a special action in the finalize method
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankur, Marcela
Thanks for your time.
Actually following question and its answer confused me , which was from old thread on this discussion forum, now I am clear.
Thanks a lot.
Question:
If we don't code finalize(), will JVM execute the default finalize() method in Object class
Answer:
No..JVM will try to execute the inherited finalize() in the super class, if any.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!