Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about finalize method?

 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all

It is possible to overload finalize method ?
If so how?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no you can't overload it.
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50240
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50240
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and we can't overload finalize method right ?
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 50240
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Rendra Basuki
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50240
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic