0

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:

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) && ( getClass() == obj.getClass())) {
            RoomOccupancy d = ( RoomOccupancy ) obj;

            if (( numberInRoom == d.numberInRoom) &&
                ( totalNumber == d.totalNumber) &&
                ( 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" );
    }
}

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();


    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
B K
  • 1
  • 1
  • 2
    `finalize` is deprecated since Java 9. Although you can still use it, it is not recommended to do so and it might be removed in future. What exactly do you want to do in this method that you can't do elsewhere? – Kartik Nov 07 '18 at 02:36
  • If you are interested why it is deprecated, [documentation](https://docs.oracle.com/javase/9/docs/api/java/lang/Object.html#finalize--). You don't need if inside finalized it will be called only once per object! It is a good practice to use @Override. You don't need `this.numberInRoom = 0; this.totalNumber = 0; this.roomCharges = 0; this.finalizeCalled = false;` in constructor. These are default values and will be assigned automatically. – uli Nov 07 '18 at 02:42
  • _" I have two files and I'm required to call the 'finalize' method."_ Why? Use cases for finalize have always been limited, and your code shows no reason for using finalize. – Mark Rotteveel Nov 07 '18 at 18:43

1 Answers1

2

finalize is deprecated since Java 9. You can see why and what the alternatives are in the javadoc:-

Deprecated. The finalization mechanism is inherently problematic. Finalization can lead to performance issues, deadlocks, and hangs. Errors in finalizers can lead to resource leaks; there is no way to cancel finalization if it is no longer necessary; and no ordering is specified among calls to finalize methods of different objects. Furthermore, there are no guarantees regarding the timing of finalization. The finalize method might be called on a finalizable object only after an indefinite delay, if at all. Classes whose instances hold non-heap resources should provide a method to enable explicit release of those resources, and they should also implement AutoCloseable if appropriate. The Cleaner and PhantomReference provide more flexible and efficient ways to release resources when an object becomes unreachable.

You should not use this method. Actually you shouldn't need to!

If you still want to use it, ignore the warning or put the following on your method to hide the warning:-

@SuppressWarnings( "deprecation" )
Kartik
  • 7,677
  • 4
  • 28
  • 50
  • I agree 100% however I got that part wrong on my last exercise and I'm trying to do better. Just having a hard time figuring it out. – B K Nov 07 '18 at 02:47
  • @BK then just use it and ignore the warning. And may I ask exactly what "cleanup" you need to do? – Kartik Nov 07 '18 at 02:54
  • The assignment is very vague all it says is to use "The 'finalize' method". To top that off the book says nothing on the subject. lol I'll just follow what you and others have said, ignore the deprecated error and I'll just preface the exercise with the fact that it is a deprecated method. Thanks for the help. – B K Nov 07 '18 at 04:43