Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What are the Different Ways to create Object In Java

Tags: object class
There are Multiple ways to create an Object of java class. In this post we will discuss how many ways we can create an object. As a fresh programmer may know two or three ways to create an object,but there are 5 ways to create an object. we shall learn how to create objects one by one. 

1) By Using new keyword:

The new operator dynamically allocates memory for an object and returns a reference to it. This reference is then stored in the variable. Thus, in java all class objects must be dynamically allocated.

The fallowing line will describe you to declare an object of type Book.

Book mybook=new Book();

The above  statement combines  two steps,it can be rewritten like this to show more clearly

Book mybook;//declare reference to object
mybook=new Book();//allocate a Book object

The first statement declares mybook is reference to an object of type Book. mybook simply holds the memory address of the actual Book object.


2) By using Cloning process:

The process of creating exactly duplicate Object is called Cloning.Object class contains clone method to perform this.
  1. All objects can not have the capability to produce cloned objects,only cloneable objects having that capability.
  2. An Object is said to be cloneable iff the corresponding class has to implement java.lang.cloneable interface.
  3. It does not contain any method because it is a marker interface
  4. Protected native Object clone() throws CloneNotSupportedException
Example:


class Test implements Cloneable
{
int a = 20;
int b = 30;
public static void main(String arg[])throws CloneNotSupportedException
{
Test t1 = new Test();
Test t2 = (Test)t1.clone();
t1.a = 200;
t1.b = 300;
System.out.println(t2.a+"----"+t2.b); 
}
}

Output:



3) By using newInstance() of java.lang:

we can also newInstance() to create new object. It creates new instance (that means new object) that is of the same type as the invoking object. This is equivalent to using new with the class default constructor. The new object is returned.

newInstance() can throws IllegalAccessException,InstantiationException

The fallowing example shows how to create object by using newInstance()

class Test 
{
public Test()
{
System.out.println("Inside Test class constructor");
}
public void disp()
{
System.out.println("Disp() method called");   
}
}
public class instanceDemo 
{
public static void main(String args[]) 
{
try{
String st = "Test";
Class cl = Class.forName(st);
Test t = (Test)cl.newInstance();
t.disp();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();

catch(InstantiationException e) 
{
e.printStackTrace();
}
catch (IllegalAccessException e) 
{
 e.printStackTrace();
}
}

}

Output:





4) By using Factory method:

The method of java class that is capable of constructing its own java class object is called as factory method. There are two types of factory methods in java. They are
a)static factory methods
b)instance factory methods

a)static factory method:
This method is used to create object of java class outside of that class only when the class is having private constructors.

Example:  
Thread t=Thread.currentThread();
class c=Class.forName("Test");
Calendar cl=Calendar.getInstance();

These methods  are also useful to create object of a class without using existing object and directly by using class name. 

b) Instance factory method:

String s=new String("hi how are You");
String s1=s.subString(2,5);

In the above statement subString() is the method of String class returning again java.lang.String class object. These methods are useful to create new objects of a class by using existing objects and their data of same class

5) By using deserialization:

Serialization is the process of writing the state of an object to a byte stream. This is useful when you want to store the state of your program to a persistent storage area,such as a file. After some time, you may require restore these object by using the process of deserialization.
When object is created through deserialization process constructor will not be executed.

Read also: Serialization in java with example


This post first appeared on Learnprogramingbyluckysir, please read the originial post: here

Share the post

What are the Different Ways to create Object In Java

×

Subscribe to Learnprogramingbyluckysir

Get updates delivered right to your inbox!

Thank you for your subscription

×