Example of Calling Private Method From Another Class
Example of Calling Private Method From Another Class
Example of Calling Private Method From Another Class
File: A.java
1 public class A {
2 private void message(){System.out.println("hello java"); }
3 }
File: MethodCall.java
1 import java.lang.reflect.Method;
2 public class MethodCall{
3 public static void main(String[] args)throws Exception{
4
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
File: A.java
1 class A{
2 private void cube(int n){System.out.println(n*n*n);}
3 }
File: M.java
1 import java.lang.reflect.*;
2 class M{
3 public static void main(String args[])throws Exception{
4 Class c=A.class;
5 Object obj=c.newInstance();
6
7 Method m=c.getDeclaredMethod("cube",new Class[]{int.class});
8 m.setAccessible(true);
9 m.invoke(obj,4);
10 }}
Output:64
Serialization in Java
1 Serialization
2 Serializable Interface
3 Example of Serialization
4 Deserialization
5 Example of Deserialization
7 Externalizable interface
The String class and all the wrapper classes implementsjava.io.Serializable interface by
default.
java.io.Serializable interface
Serializable is a marker interface (has no body). 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. Let's see the
example given below:
1 import java.io.Serializable;
2 public class Student implements Serializable{
3 int id;
4 String name;
5 public Student(int id, String name) {
6 this.id = id;
7 this.name = name;
8 }
9 }
ObjectOutputStream class
The ObjectOutputStream class is used to write primitive data types and Java objects to
an OutputStream. Only objects that support the java.io.Serializable interface can be
written to streams.
Constructor
Important Methods
Method Description
1) public final void writeObject(Object obj) throws writes the specified object to the
IOException {} ObjectOutputStream.
2) public void flush() throws IOException {} flushes the current output stream.
3) public void close() throws IOException {} closes the current output stream.
1 import java.io.*;
2 class Persist{
3 public static void main(String args[])throws Exception{
4 Student s1 =new Student(211,"ravi");
5
6 FileOutputStream fout=new FileOutputStream("f.txt");
7 ObjectOutputStream out=new ObjectOutputStream(fout);
8
9 out.writeObject(s1);
10 out.flush();
11 System.out.println("success");
12 }
13 }
success
download this example of serialization
Deserialization in java
Deserialization is the process of reconstructing the object from the serialized state.It is
the reverse operation of serialization.
ObjectInputStream class
An ObjectInputStream deserializes objects and primitive data written using an
ObjectOutputStream.
Constructor
Important Methods
Method Description
1 import java.io.Serializable;
2 class Person implements Serializable{
3 int id;
4 String name;
5 Person(int id, String name) {
6 this.id = id;
7 this.name = name;
8 }
9 }
1 class Student extends Person{
2 String course;
3 int fee;
4 public Student(int id, String name, String course, int fee) {
5 super(id,name);
6 this.course=course;
7 this.fee=fee;
8 }
9 }
Now you can serialize the Student class object that extends the Person class which is
Serializable.Parent class properties are inherited to subclasses so if parent class is
Serializable, subclass would also be.
1 class Address{
2 String addressLine,city,state;
3 public Address(String addressLine, String city, String state) {
4 this.addressLine=addressLine;
5 this.city=city;
6 this.state=state;
7 }
8 }
1 import java.io.Serializable;
2 public class Student implements Serializable{
3 int id;
4 String name;
5 Address address;//HAS-A
6 public Student(int id, String name) {
7 this.id = id;
8 this.name = name;
9 }
10 }
Since Address is not Serializable, you can not serialize the instance of Student class.
Externalizable in java
The Externalizable interface provides the facility of writing the state of an object into a
byte stream in compress format. It is not a marker interface.