"Hi again. Today we'll briefly learn about the finalize() method. The Java Virtual Machine calls the finalize() method before destroying an object. The method is used to deallocate system resources or perform other cleanup tasks. In fact, this method is the exact opposite of a constructor in Java. You will recall that constructors are used to create objects."

"The Object class has a finalize() method, which means that every other class does too (since all Java classes derive from the Object class). You can simply implement your own finalize() method in your class."

"Here's an example:"

Example:
class Cat
{
    String name;

    Cat(String name)
    {
        this.name = name;
    }

    protected void finalize() throws Throwable
    {
        System.out.println(name + " has been destroyed");
    }
}
undefined
6
Task
New Java Syntax, level 6, lesson 3
Locked
String array in reverse order
1. Create an array of 10 strings. 2. Enter 8 strings from the keyboard and save them in the array. 3. Display the contents of the entire array (10 elements) on the screen in reverse order. Each element on a new line.

"That makes sense, Ellie."

"But you should be aware that the Java Virtual Machine decides whether to call this method. More often than not, objects created inside a method and declared garbage when the method completes are destroyed immediately without any call to finalize(). This method is more like backup than a reliable solution. The best option is to release all system resources (by setting references to other objects to null) while the object is still alive. I'll tell you more about this method's advantages and nuances later. At this point, you only need to understand two things: there is such a method, and (surprise!) it isn't always called."

undefined
6
Task
New Java Syntax, level 6, lesson 3
Locked
Array of numbers in reverse order
1. Create an array of 10 numbers. 2. Enter 10 numbers from the keyboard and write them to the array. 3. Display the elements of the array in reverse order. Display each value on a new line.