Home » Java programming language

Java ObjectStreamField getOffset() Method with Example

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

ObjectStreamField Class getOffset() method

  • getOffset() method is available in java.io package.
  • getOffset() method is used to get the offset of this ObjectStreamField field.
  • getOffset() 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.
  • getOffset() method does not throw an exception at the time of returning offset.

Syntax:

    public int getOffset();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is int, it returns the offset of this ObjectStreamField.

Example:

// Java program to demonstrate the example 
// of int getOffset() method 
// of ObjectStreamField

import java.io.*;
import java.util.*;

public class GetOffSetOfOSF {
 public static void main(String[] args) {
  // Instantiates ObjectStreamClass for Calendar
  ObjectStreamClass o_sc = ObjectStreamClass.lookup(Calendar.class);

  // By using getField() method is to get the field
  // value from Calendar 
  ObjectStreamField field1 = o_sc.getField("isTimeSet");
  ObjectStreamField field2 = o_sc.getField("isSet");

  // By using getOffset() method is to return
  // the offset of the field

  int field1_offset = field1.getOffset();
  System.out.println("field1.getOffset(): " + field1_offset);

  int field2_offset = field2.getOffset();
  System.out.println("field2.getOffset(): " + field2_offset);
 }
}

Output

field1.getOffset(): 5
field2.getOffset(): 1


Comments and Discussions!

Load comments ↻





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