Tuesday, 3 June 2014

How many ways to create an Object in java?

        As we know that, Object is real world entity and it may be anything. It's specimen of class. This is special question for freshers to 2 years of experience candidates. Look the below ways and prepare well for interview.

        There are four ways to create an object in java, below is the explanation of each way with examples.

1) Using new keyword

            
       This is the most common way to create an object in java.  Almost 99% of objects are created in this way.

   e.g    Employee emp=new  Employee();

package com.adnjavainterview;

public class Employee {

     public String getName() {
          System.out.println("Object Creation Example");
     }

     public static void main(String[] args) {
           Employee emp = new Employee();
           emp.getName();
     }
}

Output:- Object Creation Example


2) Using Class.forName()

     
          If we know the name of the class & if it has a public default constructor we can create an object in this way.  The Class.forName() method loads the class  and newInstance()  method of class we can create object in Java.

        Employee obj =(Employee)  Class..forName("com.adn.Employee").newInstance();


package com.adnjavainterview;

public class Employee {

     public String getName() {
          System.out.println("Object Creation Example");
     }

     public static void main(String[] args) {
           Employee emp = Class.forName("Employee").newInstance();
           emp.getName();
     }
}

Output:- Object Creation Example


3) Using clone()

        
         The clone() can be used to create a copy of an existing object. The clone() method copies all contents of previous objects into newly created object. If you use the clone() method, you should implement the Cloneable interface.

                 Employee emp=new Employee();
               Employee cloneObject=emp.clone();


public class Employee implements Cloneable { 
      @Override
      protected Object clone() throws CloneNotSupportedException { 
            return super.clone(); 
      } 
     
      public String getName() {
            System.out.println("Object Creation Example");
      }  
    
      public static void main(String[] args) { 
           Employee emp = new Employee(); 
           try { 
                Employee cloneObject = (Employee) emp.clone(); 
                System.out.println(cloneObject.getName()); 
           } 
           catch (CloneNotSupportedException e) { 
                e.printStackTrace(); 
           } 
      } 
} 
Output:- Object Creation Example


4) Using object deserialization

   
   Object deserialization is nothing but creating an object from its serialized form.

               ObjectInputStream  inStream = new  ObjectInputStream(anInputStream);
                                     MyObject object = (MyObject) inStream.readObject(); 



 Related post:
1) Java Object Class and its methods
2) What is abstract class and interface? Difference between them.
3) What are the immutable classes in Java? How to create immutable class and What are the conditions?
4) String Interview Questions and Answers

4 comments: