Need for finalize() in Java

0 votes

I have a Java class which uses finalize() to write into a file. This method is invoked by overriding it. But my issue is, each time I execute the program, finalize() doesn’t execute. Please explain why so?

May 8, 2018 in Java by 93.lynn
• 1,550 points
63 views

1 answer to this question.

+1 vote

finalize() is a method called by the garbage collector. It ensures that there are no more references left. Basically, it is called to perform the clean-up function which can be like shutting down the database connection, network connection or closing any other resource connections. Once the finalize() is completes its execution, the Garbage Collector immediately destroys that object.

Syntax of finalize() in the Object class is:

protected void finalize throws Throwable{}

Later, you need to override this method to customize it, according to your needs.

Also, this method is generally called when an object is left unattended for a long period of time so chances are there that it might be never called in a lifetime of a program.

answered May 8, 2018 by code.reaper12
• 3,450 points

Related Questions In Java

0 votes
1 answer

I am learning looping statements. Can you tell me how 'for-each' works in Java?

While programming we often write code that ...READ MORE

answered Apr 17, 2018 in Java by Rishabh
• 3,520 points
39 views
0 votes
1 answer

concat() vs “+” operator : In Java for String concatenation

Basically, there are two important differences between ...READ MORE

answered Apr 27, 2018 in Java by Rishabh
• 3,520 points
674 views
0 votes
1 answer

What is the concept of Immutability for strings in Java ? Why are strings immutable ?

According to Effective Java, chapter 4, page 73, ...READ MORE

answered May 11, 2018 in Java by Rishabh
• 3,520 points
124 views
0 votes
1 answer

What is the 'instanceof' operator used for in Java?

It's an operator that returns true if ...READ MORE

answered May 23, 2018 in Java by Rishabh
• 3,520 points
50 views
0 votes
1 answer

How do I fix a NullPointerException?

When you declare a reference variable (i.e. ...READ MORE

answered Apr 13, 2018 in Java by parth295
• 4,600 points
68 views
0 votes
2 answers

Does the finally block always execute in Java?

public static void main(String[] args) { ...READ MORE

answered Aug 8, 2018 in Java by Sushmita
• 6,840 points
223 views
0 votes
1 answer

Play MP3 files usng Java API

I don't believe the java AudioStream class ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,520 points
1,811 views
0 votes
1 answer

What is ArrayIndexOutOfBoundsException ?

If You check System.out.print(names.length); you will get 3 ...READ MORE

answered May 11, 2018 in Java by sharth
• 3,320 points
49 views
0 votes
1 answer

Overloaded method for null in Java

The method invoked here will be the ...READ MORE

answered May 23, 2018 in Java by code.reaper12
• 3,450 points
52 views
0 votes
1 answer

“Missing return statement” within if / for / while in Java

Putting a return statement in any of the ...READ MORE

answered Sep 26, 2018 in Java by code.reaper12
• 3,450 points
938 views