From: Sai Babu <saibabu.k...@gmail.com>
Hi ,
This saibabu,
In how many ways we can create objects in java..?
Ans:there are 4 ways to create an object in java,those are like this
1)By Using new Opereator
Ex:Employe e=new Employe();
2)By Using factory method:
Ex:NumberFormat obj=NumberFormat.getNumberInstance();
:Thread t=Thread.currentThread();
here we are creating Thread class obj using the factory method of currentThread().
3)By Using newInstance():
Ex:Class c=Class.forName("Employe");
Employe e=(Employe)c.newInstance();
4).By Using clonning:
Ex:Employe obj=new Employe();
Employe obj1=(Employe)obj.clone();
clonning means:creating exact copy of an existing obj is known as clonning.
Thanks & Regards
SaiBabu
From : sai allaparthi
they are multiple ways we can create object in java
1)by using new key word
2)by using static factory method
3)by using instance factory method
4)by using factory pattern
5)by using new instance method
6)by using cloning method
7)by using deserializtion process
1)new key word:
ex: vehicle a=new vehicle();
2)static factory method: a method in java class that is capable of constructing & returning its own java class object is called as static factory method
Thread t=Thread.currentThread();
String st=String.valueOf(10);
3)using instance factory method:
String s=new String("hello");
String s1=s.concat("how r u");
static factory method will be called without object instance factory method will be called by using object.
in order to create new object for a java class by using existing object of and its data ,takes the support of instance factory method.