CodeGym /Courses /Java Syntax /Finalizing with finalize

Finalizing with finalize

Java Syntax
Level 6 , Lesson 3
Available

"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");
    }
}
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."

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.
Comments (14)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Hisham Thabet Level 8, Lahj, Yemen
9 August 2021
For more understanding you can read this : Finalize() Method
Maryam Roudbari Level 41
18 August 2020
I don't understand what is line 5 exactly doing.
Maryam Roudbari Level 41
18 August 2020
got it! it's a constructor, duh.
Ahmad Level 7, Detroit, United States
3 April 2021
You are now level 41! Congrats! Just curious how is your Java skills now?
Angelo Tratsis Level 8, Boston, United States
22 April 2021
I would love to know also
Mohamed Aldawi Level 16, Dubai, United Arab Emirates
1 December 2019
finalize() method is Deprecated(since="9")
29 December 2019
it's a warning so for now you can ignore n i tried using the command javac ClassFile.java -Xlint
ashgolan Level 8, Ashdod, Israel
8 September 2019
ok
Steven K. Sharp Level 7, Miamisburg, United States
7 September 2019
"But you should be aware that the Java Virtual Machine decides whether to call this method." This is not true. If you implement the finalize method, it will be called once the object is eligible for garbage collection. As to when it is called is nondeterministic. This is just one of the problems with finalize. Without implementing it, the JVM is able to reclaim objects when necessary. By implementing finalize, the JVM is forced to keep track of the object and handle it specially when performing garbage collection. When an object is unreachable (read the article about garbage collection), if it has implemented finalize it is placed in a special queue. It is then at the discretion of the JVM as to when it actually invokes an object's finalize method. As @Berkson posted, it is deprecated and while I understand it is here for completeness, use of it should be discouraged. Extremely discouraged.
P.B.Kalyan Krishna Level 22, Guntur, India
4 June 2019
I understand that finalize() may be called by the JVM, irrespective of being explicitly written (by overriding the Object class method) or not. And if we deliberately override the method, then our version will be called by the JVM .
Berkson Level 17, Fortaleza, Brazil
18 May 2019
The method is Deprecated since java 9. "The Cleaner and PhantomReference provide more flexible and efficient ways to release resources when an object becomes unreachable." From Oracle
Hashirama Level 26, Port-Harcourt, Nigeria
11 January 2019
There is such a non reliable method that runs a block or line of code before our object is destroyed; got it.
Mohamed Hamda Level 22, Tunis, Tunisia
12 January 2019
The "finalize" method is always reliable, I mean this method does its job whenever called, but it's the JVM who decides whether to call this methode or not.
Hashirama Level 26, Port-Harcourt, Nigeria
12 January 2019
Oh... I see.... Thanks