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 9, 2018 in Java by 93.lynn
• 1,600 points
691 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 9, 2018 by code.reaper12
• 3,500 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,620 points
936 views
+1 vote
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,620 points
4,751 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,620 points
1,607 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,620 points
1,075 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 Parth
• 4,640 points
936 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,920 points
2,062 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,620 points
9,474 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,370 points
955 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,500 points
909 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 27, 2018 in Java by code.reaper12
• 3,500 points
9,069 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP