Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

finalize()  RSS feed

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys
please tell me where iam wrong in the below code
 
Bartender
Posts: 11445
18
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?
 
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)
 
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
 
Marshal
Posts: 59728
187
  • 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
Boost this thread!