Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finalize()

 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys
please tell me where iam wrong in the below code
 
Maneesh Godbole
Saloon Keeper
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try it out?
Did it compile?
 
Varun Chopra
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
harilal ithikkat
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic