Public class Employee implements java.io. Suppose that we have the following Employee class, which implements the Serializable interface − Example To demonstrate how serialization works in Java, I am going to use the Employee class that we discussed early on in the book. Java Reflection is a mechanism for manipulating classes, interfaces, and objects at the runtime of the Java program. The return value is Object, so you will need to cast it to its appropriate data type. This method retrieves the next Object out of the stream and deserializes it. Public final Object readObject() throws IOException, ClassNotFoundException Similarly, the ObjectInputStream class contains the following method for deserializing an object − The above method serializes an Object and sends it to the output stream. This is a concept that is not used commonly but is very helpful in various frameworks of Java. Public final void writeObject(Object x) throws IOException Reflection in Java with example By Aakash Puri In this tutorial, you are going to learn about the concept of Reflection API (Application Program Interface) in Java. or loading classes with Class.forName(String) at run time requires preparing additional. The ObjectOutputStream class contains many write methods for writing various data types, but one method in particular stands out − Examining and accessing program elements through. It provides the ability to inspect classes, interfaces, fields and methods at runtime, without knowing much about. Java reflection is an API which is used to express code which is able to inspect other code in the same system. Most impressive is that the entire process is JVM independent, meaning an object can be serialized on one platform and deserialized on an entirely different platform.Ĭlasses ObjectInputStream and ObjectOutputStream are high-level streams that contain the methods for serializing and deserializing an object. Dictionary meaning of reflection: The throwing back by a body or surface of light, heat, or sound without absorbing it. Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.Īfter a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |