ObjectInputStream registerValidation() Method in Java

In this tutorial, you will learn how to use the registerValidation() method in Java.

First, you should know about the ObjectInputStream class which contains registerValidation() method before we go forward.

ObjectInputStream in Java is used to recover those objects previously serialized using an ObjectOutputStream. Another use is to pass objects between hosts using a socket stream for remote communication. Using with FileOutputStream and FileInputStream respectively allows ObjectOutputStream and ObjectInputStream to store graphs of objects.

java.io package contains ObjectInputStream class which further contains the registerValidation() method. Firstly import this package. This method registers an object to be checked before the graph is returned back. This is a nonstatic method. Therefore class object is used to access this method. It functions like resolveObject().

NOTE: This method creates files on the hard disk. As a result, This method doesn’t work on an online IDE.

registerValidation() Syntax

Below is the syntax for this method:

public void registerValidation(ObjectInputValiadation object, int order);

In the above syntax, you can see two parameters. They are:

  • ObjectInputValiadation object: Here you send an object for which you need validation.
  • int order: Object priority in the list.

Return type for registerValidation()

It returns nothing. Therefore its return type is void


  • NotActiveException
  • InvalidObjectException


See the example code below:

import java.io.*;   
 class Demo extends ObjectInputStream   
    public Demo(InputStream in) throws IOException   
  public class Example  
    public static void main(String[] args)   
        String str1 = "Welcome";   
            FileOutputStream out = new FileOutputStream("Codespeedy.txt");   
            ObjectOutputStream in = new ObjectOutputStream(out);   
            ObjectInputStream objIn  = new ObjectInputStream(new FileInputStream("codespeedy.txt"));   
            in.writeObject(new Check());   
            Check obj = (Check) objIn.readObject();   
            System.out.println("read value : " + obj.str2);   
            if (Check.str2.equals("Codespeedy"))  
                System.out.println("Something wrong ");   
        catch (Exception excpt)   
    static class Check implements Serializable, ObjectInputValidation   
        static String str2 = "Codespeedy";   
        private String readObject(ObjectInputStream in)   
        throws IOException, ClassNotFoundException   
            ObjectInputStream.GetField getfield = in.readFields();   
            in.registerValidation(this, 0);   
              return (String) getfield.get("str2", null);   
        public void validateObject() throws InvalidObjectException   
            throw new UnsupportedOperationException("Not supported yet.");   

And after we run the above Java code, we can see the results just like given below:

read value : Codespeedy


Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *