title: Problems with finalize method in Java
body: <p>I've been having problems with finalize method in my last exercises.  Seems like I have a deprecated method and I can't seem to find the newest version of finalize.  I have two files and I'm required to call the 'finalize' method. Can someone give me an example of a finalize method or help me with mine? Here is my code:</p>
  
  <pre><code>public class RoomOccupancy {
  
      private int numberInRoom;
      private static int totalNumber;
      private double roomCharges;
      private boolean finalizeCalled;
  
      public RoomOccupancy () {
          this.numberInRoom = 0;
          this.totalNumber = 0;
          this.roomCharges = 0;
          this.finalizeCalled = false;
          System.out.println ( "Room Occupancy - default/no argument constructor" );
      }
  
      public RoomOccupancy (int number, double rCharges) {
          numberInRoom = number;
          roomCharges = rCharges;
          finalizeCalled = false;
          System.out.println ( "Room Occupancy - Overloaded/2-argument constructor" );
      }
  
      public static int getTotal () {
          return totalNumber;
      }
  
      public void addOneToRoom () {
          numberInRoom++;
          totalNumber++;
      }
  
      public void removeOneFromRoom () {
          numberInRoom--;
          totalNumber--;
      }
  
      public int getNumber () {
          return numberInRoom;
      }
  
      public void setNumber (int number) {
          this.numberInRoom = number;
      }
  
      public double getCharges () {
          return roomCharges;
      }
  
      public void setCharges (double rCharges) {
          this.roomCharges = rCharges;
      }
  
      public String toString () {
          String c;
          c = " " + numberInRoom + " " + roomCharges + " ";
          return c;
      }
  
      public boolean equals ( Object obj ) {
          if ( this == obj ) return true;
  
          if (( obj != null) &amp;&amp; ( getClass() == obj.getClass())) {
              RoomOccupancy d = ( RoomOccupancy ) obj;
  
              if (( numberInRoom == d.numberInRoom) &amp;&amp;
                  ( totalNumber == d.totalNumber) &amp;&amp;
                  ( roomCharges == d.roomCharges)) {
                  return true;
              }
              else {
                  return false;
              }
  
          }
          else {
              return false;
          }
  
      }
  
      public void finalize ( ) {
          if ( !finalizeCalled ) {
              // Do cleanup
          }
          System.out.println ( "Course - finalize method" );
  
      }
  
      public void dispose ( ) {
          //Do cleanup
          finalizeCalled = true;
          System.out.println ( "Course - finalize method" );
      }
  }
  </code></pre>
  
  <p></p>
  
  <pre><code>import java.text.DecimalFormat;
  
  public class RoomOccupancyTest {
  
      public static void main ( String [] args ) {
  
          RoomOccupancy noOccupancy = new RoomOccupancy ( );
          System.out.println ("No Occupancy =" + noOccupancy);
          RoomOccupancy roomA = new RoomOccupancy(0, 100.00);
          RoomOccupancy roomB = new RoomOccupancy(0, 200.00);
          RoomOccupancy roomC = new RoomOccupancy(0, 250.00);
          DecimalFormat patternCharges = new DecimalFormat("#####0.00");
  
          System.out.println ("Five people have checked into room A at $100.00 per person\n" +
                              "Four people have checked into room B at $200.00 per person.\n" +
                              "Three people have checked into room C at $250.00 per person.");
          roomA.addOneToRoom();
          roomA.addOneToRoom();
          roomA.addOneToRoom();
          roomA.addOneToRoom();
          roomA.addOneToRoom();
          roomB.addOneToRoom();
          roomB.addOneToRoom();
          roomB.addOneToRoom();
          roomB.addOneToRoom();
          roomC.addOneToRoom();
          roomC.addOneToRoom();
          roomC.addOneToRoom();
  
          System.out.println ("Room A holds " + roomA.getNumber() + " The total charge is: $" + (patternCharges.format(roomA.getCharges() * roomA.getNumber())));
          System.out.println ("Room B holds " + roomB.getNumber() + " The total charge is: $" + (patternCharges.format(roomB.getCharges() * roomB.getNumber())));
          System.out.println ("Room C holds " + roomC.getNumber() + " The total charge is: $" + (patternCharges.format(roomC.getCharges() * roomC.getNumber())));
          System.out.println ("Total in all rooms is " + RoomOccupancy.getTotal());
  
          System.out.println ("One person from each room has left.");
          roomA.removeOneFromRoom();
          roomB.removeOneFromRoom();
          roomC.removeOneFromRoom();
  
          System.out.println ("Room A holds " + roomA.getNumber() + " The total charge is: $" + (patternCharges.format(roomA.getCharges() * roomA.getNumber())));
          System.out.println ("Room B holds " + roomB.getNumber() + " The total charge is: $" + (patternCharges.format(roomB.getCharges() * roomB.getNumber())));
          System.out.println ("Room C holds " + roomC.getNumber() + " The total charge is: $" + (patternCharges.format(roomC.getCharges() * roomC.getNumber())));
          System.out.println ("Total in all rooms is " + RoomOccupancy.getTotal());
  
          if (roomA == roomB)
              System.out.println ("Room A and B are the same object."); // NOT EXPECTED
          if (roomA == roomC)
              System.out.println ("Room A and C are the same object."); // NOT EXPECTED
          if (roomB == roomC)
              System.out.println ("Room B and C are the same object."); // NOT EXPECTED
          else
              System.out.println ("No rooms are the same object."); // EXPECTED
  
          if (roomA.equals (roomB))
              System.out.println ( "Room A and B are EQUAL."); // NOT EXPECTED
          if (roomA.equals(roomC))
              System.out.println ( "Room A and C are the EQUAL."); // NOT EXPECTED
          if (roomB.equals(roomC))
              System.out.println ( "Room B and C are the EQUAL."); // NOT EXPECTED
          else
              System.out.println ( "No Rooms are EQUAL."); // EXPECTED
  
          roomA.finalize();
          roomB.finalize();
          roomC.finalize();
  
          roomA = roomB = roomC = null;
          System.gc();
  
  
      }
  }
  </code></pre>