Home » Java programming language

Java ObjectOutputStream replaceObject() Method with Example

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

ObjectOutputStream Class replaceObject() method

  • replaceObject() method is available in package.
  • replaceObject() method is used to permit all trusted child classes of ObjectOutputStream to substitute the given object for another while serializing. Replacing objects is deactivated until enableReplaceObject() character that the stream requesting to do replacement can be trusted. The first occurrence of every object written in the serialization stream is passed to replaceObject. Subsequent references to the objects are replaced by the object retrieved by the call to replaceObject. It allows only trusted streams to replaceObject.
  • replaceObject() 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.
  • replaceObject() method may throw an exception at the time of replacing the object.
    IOException: This exception may throw when getting any input/output error while writing to the output stream.


    protected Object replaceObject(Object o);


  • Object o – represents the object is to be replaced with.

Return value:

The return type of this method is Object, it returns another alternative object that replaced the given object.


// Java program to demonstrate the example 
// of Object replaceObject(Object o) method of 
// ObjectOutputStream


public class ReplaceObject extends ObjectOutputStream {
 public ReplaceObject(OutputStream os_stm) throws IOException {

 public static void main(String[] args) throws IOException {
  String str = "Java World!!!";

  try {
   // Instantiates ObjectOutputStream , ObjectInputStream 
   // FileInputStream and FileOutputStream
   FileOutputStream file_out_stm = new FileOutputStream("D:\\includehelp.txt");
   ReplaceObject obj_out_stm = new ReplaceObject(file_out_stm);
   FileInputStream file_in_stm = new FileInputStream("D:\\includehelp.txt");
   ObjectInputStream obj_in_stm = new ObjectInputStream(file_in_stm);

   //obj_out_stm.writeObject(new ReadObject()); 

   // By using enableReplaceObject() method is
   // to enable to reolace object
   System.out.println("enableReplaceObject() invoked: ");
   boolean status = obj_out_stm.enableReplaceObject(true);
   System.out.println("obj_in_stm.enableReplaceObject(true): " + status);

   Object o = obj_out_stm.replaceObject("Java Programmer");
   System.out.println("obj_out_stm.replaceObject(Java Programmer): " + o);
  } catch (Exception ex) {


enableReplaceObject() invoked: 
obj_in_stm.enableReplaceObject(true): false
obj_out_stm.replaceObject(Java Programmer): Java Programmer


Comments and Discussions



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

© some rights reserved.