- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - ObjectStreamField toString() method
Description
The Java ObjectStreamField toString() method returns a string that describes this field.
-
The toString() method returns a textual description of the field, including −
Field name
Field type
An optional unshared flag if applicable
It's helpful for debugging, introspecting serialized fields, or generating simple logs.
Declaration
Following is the declaration for java.io.ObjectStreamField.toString() method.
public String toString()
Parameters
NA
Return Value
This method returns a string representation of the object.
Exception
NA
Example - Usage of ObjectStreamField toString() method
The following example shows the usage of ObjectStreamField toString() method.
ObjectStreamFieldDemo.java
package com.tutorialspoint;
import java.io.ObjectStreamClass;
import java.io.ObjectStreamField;
import java.util.Calendar;
public class ObjectStreamFieldDemo {
public static void main(String[] args) {
// create a new object stream class for Integers
ObjectStreamClass osc = ObjectStreamClass.lookup(Integer.class);
// get the field value from Integer class
ObjectStreamField field = osc.getField("value");
// print field as a string
System.out.println("" + field.toString());
// create a new object stream class for calendar
ObjectStreamClass osc2 = ObjectStreamClass.lookup(Calendar.class);
// get the field value from Integer class
ObjectStreamField field2 = osc2.getField("isSet");
// print field as a string
System.out.println("" + field2.toString());
}
}
Output
Let us compile and run the above program, this will produce the following result −
I value [Z isSet
Example - Print toString() for each serializable field in a class
The following example shows the usage of ObjectStreamField toString() method. We're printing all serializable fields of a User class using ObjectStreamField.toString().
ObjectStreamFieldDemo.java
package com.tutorialspoint;
import java.io.ObjectStreamClass;
import java.io.ObjectStreamField;
import java.io.Serializable;
public class ObjectStreamFieldDemo {
public static void main(String[] args) {
ObjectStreamClass osc = ObjectStreamClass.lookup(User.class);
ObjectStreamField[] fields = osc.getFields();
System.out.println("Fields in User class:");
for (ObjectStreamField field : fields) {
System.out.println(" - " + field.toString());
}
}
static class User implements Serializable {
private static final long serialVersionUID = 1L;
String username;
int age;
transient String password; // not serialized
}
}
Output
Let us compile and run the above program, this will produce the following result−
Fields in User class: - I age - Ljava/lang/String; username
Explanation
toString() automatically formats the field name and type.
The transient field password is not shown, because it is not part of serialization.
Example - Create custom fields and call toString() directly
The following example shows the usage of ObjectStreamField toString() method. We're manually create ObjectStreamField instances and call toString().
ObjectStreamFieldDemo.java
package com.tutorialspoint;
import java.io.ObjectStreamField;
public class ObjectStreamFieldDemo {
public static void main(String[] args) {
ObjectStreamField field1 = new ObjectStreamField("id", int.class);
ObjectStreamField field2 = new ObjectStreamField("name", String.class);
ObjectStreamField field3 = new ObjectStreamField("active", boolean.class);
System.out.println("Manually created fields:");
System.out.println(" - " + field1.toString());
System.out.println(" - " + field2.toString());
System.out.println(" - " + field3.toString());
}
}
Output
Let us compile and run the above program, this will produce the following result−
Manually created fields: - I id - Ljava/lang/String; name - Z active
Explanation
toString() clearly identifies each field's type and name.
This is useful when building or analyzing custom serialization metadata.