Required methods of Method class

1) public void setAccessible(boolean status) throws SecurityException sets the
accessibility of the method.

2) public Object invoke(Object method, Object... args) throws
IllegalAccessException, IllegalArgumentException, InvocationTargetException is
used to invoke the method.

Required method of Class class
1) public Method getDeclaredMethod(String name,Class[] parameterTypes)throws
NoSuchMethodException,SecurityException: returns a Method object that reflects the
specified declared method of the class or interface represented by this Class object.

Example of calling private method from another class
Let's see the simple example to call private method from another class.

1 public class A {
2 private void message(){System.out.println("hello java"); }
3 }
1 import java.lang.reflect.Method;
2 public class MethodCall{
3 public static void main(String[] args)throws Exception{
5 Class c = Class.forName("A");
6 Object o= c.newInstance();
7 Method m =c.getDeclaredMethod("message", null);
8 m.setAccessible(true);
9 m.invoke(o, null);
10 }
11 }
Output:hello java

Another example to call parameterized private method
from another class
Let's see the example to call parameterized private method from another class


setAccessible(true). 8 1 import java. 10 }} Output:64 .println(n*n*n).reflect. 5 Object obj=c.getDeclaredMethod("cube". 6 7 Method m=c.class}).*. 9 m.lang. 2 class M{ 3 public static void main(String args[])throws Exception{ 4 Class Class[]{int.4).out.class.newInstance().1 class A{ 2 private void cube(int n){System.invoke(obj.} 3 } File: M.

Serializable interface by default. Advantage of Java Serialization It is mainly used to travel object's state on the network (known as marshaling).Serializable. RMI. java. The String class and all the wrapper classes implementsjava. It is mainly used in Hibernate. Let's see the example given below: 1 import java. JPA. JMS Serialization in Java 1 Serialization 2 Serializable Interface 3 Example of Serialization 4 Deserialization 5 Example of Deserialization 6 Serialization with Inheritance 7 Externalizable interface 8 Serialization and static datamember Serialization in java is a mechanism of writing the state of an object into a byte stream. The reverse operation of serialization is called deserialization. It is just used to "mark" java classes which support a certain capability. It must be implemented by the class whose object you want to persist.Serializable interface Serializable is a marker interface (has no body).io. .

Important Methods Method Description 1) public final void writeObject(Object obj) throws writes the specified object to the IOException {} ObjectOutputStream.Serializable interface can be written to streams.2 public class Student implements Serializable{ 3 int id.txt. Only objects that support the java.*. 5 . 2) public void flush() throws IOException {} flushes the current output stream. Example of Java Serialization In this = id. we are going to serialize the object of Student = name. 8 } 9 } ObjectOutputStream class The ObjectOutputStream class is used to write primitive data types and Java objects to an OutputStream. The writeObject() method of ObjectOutputStream class provides the functionality to serialize the object. 3) public void close() throws IOException {} closes the current output stream. 5 public Student(int id. 1 import We are saving the state of the object in the file named f. String name) { 6 this. 4 String 7 this. Constructor 1) public ObjectOutputStream(OutputStream out) throws IOException {}creates an ObjectOutputStre writes to the specified OutputStream. 2 class Persist{ 3 public static void main(String args[])throws Exception{ 4 Student s1 =new Student(211."ravi").

10 out. 2 class Depersist{ 3 public static void main(String args[])throws Exception{ 4 5 ObjectInputStream in=new ObjectInputStream(new FileInputStream("f.6 FileOutputStream fout=new FileOutputStream("f.*.writeObject(s1). . reads an object from th ClassNotFoundException{} stream. Important Methods Method Description 1) public final Object readObject() throws IOException. 2) public void close() throws IOException {} closes ObjectInputStrea Example of Java Deserialization 1 import java.out.readObject().It is the reverse operation of serialization.txt").txt")). 11 System. 12 } 13 } success download this example of serialization Deserialization in java Deserialization is the process of reconstructing the object from the serialized state.println("success"). Constructor 1) public ObjectInputStream(InputStream in) throws creates an ObjectInputStream that re IOException {} the specified InputStream. ObjectInputStream class An ObjectInputStream deserializes objects and primitive data written using an ObjectOutputStream. 7 ObjectOutputStream out=new ObjectOutputStream(fout). 8 9 out.flush().io. 6 Student s=(Student)in.

Parent class properties are inherited to subclasses so if parent class is Serializable. 7 = id.println(s. 6 this. 4 public Student(int id. 7 this. 8 } 9 } Now you can serialize the Student class object that extends the Person class which is Serializable. Let's see the example given below: 1 import java. 3 int String course.close().out. String name) { 6 = name. 2 class Person implements Serializable{ 3 int id. 8 9 in. 5 Person(int id. 4 String name. 10 } 11 } 211 ravi download this example of deserialization Java Serialization with Inheritance (IS-A Relationship) If a class implements serializable then all its sub classes will also be Java Serialization with Aggregation (HAS-A Relationship) .fee=fee. int fee) { 5 super(id. 8 } 9 } 1 class Student extends Person{ 2 String course. String" "+s. subclass would also be.7 System. NotSerializableException is thrown at runtime. String name) { 6 this. If a class has a reference of another class.//it won't be serialized 5 public Student(int id. In such case. 1 class Address{ 2 String addressLine.addressLine=addressLine. 5 this. 4 String name. 7 this.state=state. 8 this.state. it will not be serialized because static is the part of class not object. String name) { 7 this. String state) { 4 this. 1 class Employee implements Serializable{ 2 int id. 2 public class Student implements Serializable{ 3 int = = = id. 9 } 10 } Since Address is not Serializable. 8 } 9 } .name = name. 5 Address Java Serialization with static data member If there is any static data member in a class. all the references must be Serializable otherwise serialization process will not be performed. 7 } 8 } 1 import java. 3 String 6 this.//HAS-A 6 public Student(int id. 4 static String company="SSS IT Pvt Ltd". Note: All the objects within an object must be Serializable. you can not serialize the instance of Student class. 3 public Address(String addressLine. String city.

serialization will be failed.Java Serialization with array or collection Rule: In case of array or collection. Externalizable in java The Externalizable interface provides the facility of writing the state of an object into a byte stream in compress format. all the objects of array or collection must be serializable. The Externalizable interface provides two methods:  public void writeExternal(ObjectOutput out) throws IOException  public void readExternal(ObjectInput in) throws IOException . If any object is not serialiizable. It is not a marker interface.