It's not a secret anymore!
The moose likes Beginning Java and the fly likes finalize() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "finalize() " Watch "finalize() " New topic


Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 241
hi guys
please tell me where iam wrong in the below code
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10826

Did you try it out?
Did it compile?

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211
finalize() method has protected access in Object. This means that finalize() method can be called only

1. By a subclass of Object, hence following code will work

2. From a method within Object class

3. From a class in same package as Object class (a class in java.lang package)

-Varun -
(My Blog) - Online Certifications - Webner Solutions
harilal ithikkat
Ranch Hand

Joined: Oct 06, 2008
Posts: 221
finalize is a protected method..
So you cannot access it using object,But you can do by inheriting.
It will throw some Exception(java.lang.Throwable).you need to catch it

SCJP 1.5
"A candle looses nothing by lighting another candle"
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46397
The Throwable is already taken care of because the main method declares it with a throws.

The only real use for the finalize() method is to release resources bound by any "native" (usually C or C++) code; if you are on beginner's you have probably never heard of using "native" code! If you have any resources (eg file reading) bound by Java code, it is usual to close them in the same method they were opened in.

So you ought only to use the finalize() method for Campbell's Do-It-Yourself all-purpose excuse: "I only wanted to see what happens if . . ." That is an adequate excuse for almost anything short of corrupting or deleting system files! Try overriding a finalize() method to readand see what happens. Another thing covered by Campbell's all-purpose excuse

In a real-life application you would leave all invocations of finalize to the JVM, and never actually call it yourself. You can read about finalize here, but it is by no means easy to understand.
I agree. Here's the link:
subject: finalize()
jQuery in Action, 3rd edition