0
Sponsored Links


Ad by Google
Basic Java interview question, This types of questions mostly asked at fresher or very less experienced level. If you are thinking about to jump inside the world of Java or you have 2-3 years of experience in java. Then you must have to know the answer of this question, and what are the ways to create an object in java.

Well Technically 5 ways to create an object in java:

1. new keyword.

Using new keyword, which is very common approach to create an object.

2. Reflection (Class.forName().newInstance() method).

Using reflection api. with the help of Class.forName("class") load the class and then newInstance() method to create an object.

3. With the help of class loader.

Using getClassLoader().load() method which will again load the class and then call newInstance() method to create an object.

clone()

If your class implements Cloneable interface then use clone() method to create an object.
Note: clone() method will never call your constructor to create an object.

5. deserialization

Using deserialization you are creating an object.
Note:  deserialization will never call your constructor.

See the below program to complete example of above all the different ways of creating an object.

FIFASoccer.java

package com.test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class FIFASoccer implements Cloneable,Serializable {

 static int objectNumber;
 
 public FIFASoccer(){
 System.out.println("Created Object number="+ ++objectNumber); 
 }
 public static void main(String[] args) {
  // using new keyword
  FIFASoccer objOne = new FIFASoccer();
  System.out.println("hashCode of objOne="+objOne.hashCode());
  
  try {
   // using reflection
   FIFASoccer objTwo = (FIFASoccer)Class.forName("com.test.FIFASoccer").newInstance();
   System.out.println("hashCode of objTwo="+objTwo.hashCode());
   
   //using clone() method.
   FIFASoccer objThree = (FIFASoccer) objTwo.clone();
   System.out.println("hashCode of objThree="+objThree.hashCode());
   
   // using class loader
   FIFASoccer objFour = (FIFASoccer) FIFASoccer.class.getClassLoader().
     loadClass("com.test.FIFASoccer").newInstance();
   System.out.println("hashCode of objFour="+objFour.hashCode());
   
   serialize(objFour, "fifaSoccer.ser");
   
   //using deSerialization
   FIFASoccer objFive = (FIFASoccer) deserialize("fifaSoccer.ser");
   System.out.println("hashCode of objFive="+objFive.hashCode());
   
  } catch (InstantiationException | IllegalAccessException |
    ClassNotFoundException | CloneNotSupportedException |IOException e) {
   e.printStackTrace();
  }
 }
 
  // deSerialize an Object from given file
    public static Object deserialize(String fileName) throws IOException,
            ClassNotFoundException {
        FileInputStream fis = new FileInputStream(fileName);
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object obj = ois.readObject();
        ois.close();
        return obj;
    }
 
    // serialize an object and save it to file
    public static void serialize(Object obj, String fileName)
            throws IOException {
        FileOutputStream fos = new FileOutputStream(fileName);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
 
        fos.close();
    }
}


Note: clone() method and deserialization will never call constructor to create an object.

OUT PUT:
Created Object number=1
hashCode of objOne=153609406
Created Object number=2
hashCode of objTwo=437942948
hashCode of objThree=1080668767 No constructor was called so No object number printed
Created Object number=3
hashCode of objFour=1530764290
hashCode of objFive=129655114 No constructor was called so No object number printed


Sponsored Links

0 comments:

Post a Comment