Sunday, November 23, 2025
HomeLanguagesJavaObjectInputStream enableResolveObject() method in Java with examples

ObjectInputStream enableResolveObject() method in Java with examples

The enableResolveObject() method of the ObjectInputStream class in Java is used to enables the stream to do replacement of objects read from the stream. When enabled, the resolveObject(java.lang.Object) method is called for every object being deserialized.

Syntax:

protected boolean enableResolveObject(
                        boolean enable)

Parameters: This method accepts only single parameter.

  • enable: This parameter is set to true to enable use of resolveObject() for each object being deserialized.

Return Value: This method returns the previous setting before this method was invoked.

Errors and Exceptions: The function throws SecurityException if a security manager exists and its checkPermission method denies enabling the stream to do replacement of objects read from the stream.

Below program illustrate the above method:

Program 1:

Java




// Java program to illustrate
// the above method
  
import java.io.*;
  
public class GFG
    extends ObjectInputStream {
  
    public GFG(InputStream in)
        throws IOException
    {
        super(in);
    }
  
    public static void main(String[] args)
    {
        String s = "Geeksforgeeks";
        try {
  
            // create a new file
            // with an ObjectOutputStream
            FileOutputStream out
                = new FileOutputStream("Shubham.txt");
  
            ObjectOutputStream out1
                = new ObjectOutputStream(out);
  
            // write
            out1.writeUTF(s);
  
            // Flushes the stream
            out1.flush();
  
            // create an ObjectInputStream
            // for the file
            GFG example
                = new GFG(
                    new FileInputStream("Shubham.txt"));
  
            System.out.println(
                example
                    .resolveObject(
                        example.readUTF()));
  
            System.out.println(
                example
                    .enableResolveObject(true));
        }
  
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}


Output:

Program 2:

Java




// Java program to illustrate
// the above method
  
import java.io.*;
import java.io.InputStream;
  
public class GFG
    extends ObjectInputStream {
  
    public GFG(InputStream in)
        throws IOException
    {
        super(in);
    }
  
    public static void main(String[] args)
        throws IOException
    {
  
        int[] array = { 1, 34, 23, 425, 69, 22 };
  
        try {
  
            // create a new file
            // with an ObjectOutputStream
            FileOutputStream out
                = new FileOutputStream("Shubham.txt");
  
            DataOutputStream out1
                = new DataOutputStream(out);
  
            // write
            // for each byte in the buffer
            for (int val : array) {
  
                // write character to the dos
                out1.writeInt(val);
            }
  
            // Flushes the stream
            out1.flush();
  
            // create an ObjectInputStream
            // for the file
            DataInputStream example
                = new DataInputStream(
                    new FileInputStream("Shubham.txt"));
  
            for (int i = 0; i <= array.length; i++) {
                int c = example.readInt();
                System.out.print(c + " ");
            }
            System.out.print("\n");
        }
  
        catch (Exception ex) {
        }
    }
}


Output:

Reference:

https://docs.oracle.com/javase/10/docs/api/java/io/ObjectInputStream.html#enableResolveObject(boolean)

RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6785 POSTS0 COMMENTS
Nicole Veronica
11932 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12000 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6864 POSTS0 COMMENTS
Umr Jansen
6852 POSTS0 COMMENTS