Home » Java programming language

Java ObjectInputStream enableResolveObject() Method with Example

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

ObjectInputStream Class enableResolveObject() method

  • enableResolveObject() method is available in java.io package.
  • enableResolveObject() method is used to enable the ObjectInputStream to permit objects read from the stream to be replaced. When a boolean variable is set to true and SecurityManager installed and it first invoked the checkPermission() method with SerializablePermission(“enableSubstitution”) of SecurityManager to ensure that it is ready to activate this ObjectInputStream to permit objects read from the stream ObjectInputStream to be replaced.
  • enableResolveObject() 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.
  • enableResolveObject() method may throw an exception at the time of checking object status to replace.
    SecurityException: This exception may throw when its checkPermission() method does not allow to enable the stream to permit objects read from the stream to be replaced when SecurityManager exists.

Syntax:

    public boolean  enableResolveObject(boolean status);

Parameter(s):

  • boolean status – if it sets to true that means to enable use of resolveObject() for each object being deserialized.

Return value:

The return type of the method is boolean, it gets the old setting before this method was called.

Example:

// Java program to demonstrate the example 
// of void defaultReadObject() method 
// of ObjectInputStream

import java.io.*;

public class DefaultReadObject {
 public static void main(String[] args) throws Exception {
  // Instantiates ObjectOutputStream , ObjectInputStream 
  // FileInputStream and FileOutputStream
  FileOutputStream file_out_stm = new FileOutputStream("D:\\includehelp.txt");
  ObjectOutputStream obj_out_stm = new ObjectOutputStream(file_out_stm);
  FileInputStream file_in_stm = new FileInputStream("D:\\includehelp.txt");
  ObjectInputStream obj_in_stm = new ObjectInputStream(file_in_stm);

  // By using writeObject() method is to
  // write object to Serialized class
  obj_out_stm.writeObject(new DefaultObjectClass());
  obj_out_stm.flush();

  // By using readObject() method is to
  // read an object from the Serialized class
  DefaultObjectClass def_obj = (DefaultObjectClass) obj_in_stm.readObject();

  // Using both readObject and defaultReadObject(); 
  System.out.println("obj_in_stm.defaultReadObject(): " + def_obj.str);
  System.out.println("obj_in_stm.defaultReadObject(): " + def_obj.in);
 }

 static class DefaultObjectClass implements Serializable {
  String str = "Java Programming";
  Integer in = new Integer(10);
  private void readObject(ObjectInputStream obj_stm) throws IOException, ClassNotFoundException {
   // By using defaultReadObject() method is 
   // to read non-static fields of the present 
   // class from the ObjectInputStream
   obj_stm.defaultReadObject();
  }
 }
}

Output

enableResolveObject invoked
resoveObject: Java


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.