Problems with finalize method in Java

4125 views java
2

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 

answered question

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?

If you are interested why it is deprecated, documentation. 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.

1 Answer

12

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" )

posted this

Have an answer?

JD

Please login first before posting an answer.