The obj, int prio) method is used to register an object to be validated before the graph is returned. While similar to resolveObject these validations are called after the entire graph has been reconstituted. Typically, a readObject method will register the object with the stream so that when all of the objects are restored a final set of validations can be performed.


Following is the declaration for method.

public void registerValidation(ObjectInputValidation obj, int prio)


  • obj − The object to receive the validation callback.

  • prio − Controls the order of callbacks;zero is a good default. Use higher numbers to be called back earlier, lower numbers for later callbacks. Within a priority, callbacks are processed in no particular order.

Return Value

This method does not return a value.


  • NotActiveException − The stream is not currently reading objects so it is invalid to register a callback.

  • InvalidObjectException − The validation object is null.


The following example shows the usage of method.

package com.tutorialspoint;


public class ObjectInputStreamDemo {

   public static void main(String[] args) {
      try {

         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeObject(new Example());

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // read the object and print the string
         Example a = (Example) ois.readObject();

         // print the string that is in Example class
         System.out.println("" + a.s);

         // validate the object

      } catch (Exception ex) {


   static class Example implements Serializable, ObjectInputValidation {
      String s = "Hello World!";

      private String readObject(ObjectInputStream in)
         throws IOException, ClassNotFoundException {

         // call readFields in readObject
         ObjectInputStream.GetField gf = in.readFields();

         // register validation for the object
         in.registerValidation(this, 0);

         // save the string and return it
         return (String) gf.get("s", null);


      public void validateObject() throws InvalidObjectException {
         System.out.println("Validating object...");
         if (this.s.equals("Hello World!")) {
         } else {
            System.out.println("Not validated.");

Let us compile and run the above program, this will produce the following result −

Hello World!
Validating object...


E-Books Store