Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. //: initialization/TerminationCondition.java
  2. // Using finalize() to detect an object that
  3. // hasn’t been properly cleaned up.
  4. class Book {
  5.     boolean checkedOut = false;
  6.     Book(boolean checkOut) {
  7.         checkedOut = checkOut;
  8.     }
  9.     void checkIn() {
  10.         checkedOut = false;
  11.     }
  12.     protected void finalize() {
  13.         if(checkedOut)
  14.         System.out.println("Error: checked out");
  15.         // Normally, you’ll also do this:
  16.         // super.finalize(); // Call the base-class version
  17.     }
  18. }
  19. public class TerminationCondition {
  20.     public static void main(String[] args) {
  21.         Book novel = new Book(true);
  22.         // Proper cleanup:
  23.         novel.checkIn();
  24.         // Drop the reference, forget to clean up:
  25.         new Book(true);
  26.         // Force garbage collection & finalization:
  27.         System.gc();
  28.     }
  29. } /* Output:
  30. Error: checked out
  31. *///:~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement