File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes about finalize method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about finalize method?" Watch "about finalize method?" New topic
Author

about finalize method?

Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
hi all

It is possible to overload finalize method ?
If so how?


Anto Telvin Mathew<br />Many of the life failures are people who did not realize how close they were to success when they give up. EDISON
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

no you can't overload it.
Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
But while searching in net i found an answer like this

Yes you can overload finalize method but only the following version is called by garbage collector:
protected void finalize() throws Throwable { };


Is this wrong ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
What David meant was that if you try to overload a finalize() method you will get something which looks like a finalize() method but isn't a finalize() method because it will never operate as a finalize() method.

The JVM is very specific about which methods it calls; finalize, main, writeObject (see java.io.Serializable) all have to express specific signatures for the JVM to call them.

Unlike some overloaded methods (look at java.util.Arrays#binarySearch, etc) which offer the same functionality to different users, finalize offers functionality to the JVM only, overloading would never work.

Hope that has made things clearer rather than obscured them
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
see you can override finalize method but you have to keep 2 things in mind:

1) overridden method will not be called automatically by JVM, it needs to be explicitly called.
2) also dont forget to call object Class finalize() method from the overriden method. Why you want to know? just think and let me know if you dont understand


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
I think you have misunderstood the question; it is about overloading not overriding. The overridden finalize() method is called by the JVM, and you ought not to call it explicitly. You are right that we should always start it with super.finalize();
Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
thanks for the answer

i asked question about overloading not overriding .and which one is correct since i read two statements . overridden methods are called by JVM or not
Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
and we can't overload finalize method right ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

As said before, you can overload it but the JVM won't automatically call it for you. It won't be a special method like public static main anymore, but just a regular method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
You're welcome
Rendra Basuki
Greenhorn

Joined: Nov 25, 2008
Posts: 1
Originally posted by Harshit Rastogi:
see you can override finalize method but you have to keep 2 things in mind:

1) overridden method will not be called automatically by JVM, it needs to be explicitly called.
2) also dont forget to call object Class finalize() method from the overriden method. Why you want to know? just think and let me know if you dont understand


Hi Harshit
I made a java application the override the finalize method. My intention is that this application will call another java application (login and transaction User Interface) and will be closed when user exited the login page.

The program direction is that upon starting the jar, the VM will call Main.class that will load Login.class and after login will load Transaction.class.

If your quotes above are correct, then my java should not close by itself nor calls the finalize method in the Main.class. But for some reason on some machine, the finalize method in Main.class was called although no method whatsoever calls it explicitly (unless got caught in an exception, which is not).

So, I renamed the finalize() method in Main.class and it works OK. Can you explain why this happens?

By the way the reason I used finalize in Main.class is so that if the software somehow exited unexpectedly, it will at least clean all the db connection, etc.

Thank you
Rendra
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Welcome to JavaRanch

You ought not to have posted here; this is an old thread and you have asked a completely new question. Have a look at this FAQ with a corny name.

Please post your question again as a new thread.
 
jQuery in Action, 2nd edition
 
subject: about finalize method?
 
Similar Threads
Gc question
finalize
why finalize() can't be defined as private?
difference between finally and finalize()
Finalization Method