Home » Java programming language

Java FileOutputStream finalize() Method with Example

FileOutputStream Class finalize() method: Here, we are going to learn about the finalize() method of FileOutputStream Class with its syntax and example.
Submitted by Preeti Jain, on April 01, 2020

FileOutputStream Class finalize() method

  • finalize() method is available in java.io package.
  • finalize() method is used to free connection to the file and it assures that close() method of this FileOutputStream invokes when there is none references exist to this FileOutputStream.
  • finalize() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • finalize() method may throw an exception at the time of finalizing the stream.
    IOException: This exception may throw while getting any input/output error.


    protected void finalize();


  • It does not accept any parameter.

Return value:

The return type of the method is void, it returns nothing.


// Java program to demonstrate the example 
// of void finalize() method of FileOutputStream

import java.io.*;

public class FinalizeOfFOS extends FileOutputStream {
 public FinalizeOfFOS() throws Exception {
 public static void main(String[] args) throws IOException {
  int val;

  try {
   // Instantiates FinalizeOfFOS
   FinalizeOfFOS fos_stm = new FinalizeOfFOS();

   // By using write() method is to write
   // a byte to fos_stm

   // By using finalize() method is to free
   // memory when no more references exists   

   System.out.println("finalize() invoked: ");

   // when we call write() method after
   // finalizing the stream will not result an exception
  } catch (Exception ex) {


finalize() invoked:

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.