How many ways we can create object ?

Showing Answers 1 - 72 of 72 Answers

swathi

  • Nov 22nd, 2006
 

we can create an object using 3 ways they are:

using new operator

using factory method

n using cloning technique

  Was this answer useful?  Yes

Rajesh

  • Nov 22nd, 2006
 

There are 4 ways to create an object:

1.Using 'new' operator.

2.Using class.forName("classname").newInastance();

3.Using clone();

4.Using getInstance();

Muthuramakrishnan.K

  • Dec 8th, 2006
 

By using new operator

By using factory method

n using cloning technique

By using getInstance();

By Using class.forName("classname").newInastance();

nagu

  • Dec 18th, 2006
 

As per my knowledge..

1. using new operator

2. using clone() method of Object

3. using Class.forName("className")

4. by using deserialization

I am not aware of the getInstance() method... can any one tell me it is the method of which class ?

  Was this answer useful?  Yes

santh kumar

  • Dec 19th, 2006
 

All above said ways are right. it's special for "Nagu"............ getInstance(java.lang.String classname) method is present in the class "AbstractFactory" this method returns an instance of the specified class.

  Was this answer useful?  Yes

nageswar30

  • Dec 20th, 2006
 

Thanks for reply "Santh Kumar" But still I am not able to find AbstractFactory in java api documentation (JDKTM 5.0 Documentation) ....Could you please tell me where it is.....     Does that class provided by any other third party tool ?

  Was this answer useful?  Yes

santh kumar

  • Dec 21st, 2006
 

It's correction to earlier answer made by me. AbstractFactory is not at all a class. it's a design pattern provided by third party vendor. Actually getInstance() is present in the "NumberFormat" class (java.text.NumberFormat) but this method returns the number format for the current default locale. it doesnt return a object. I'm not sure,but this method is also present in someother classes also.

  Was this answer useful?  Yes

santh kumar

  • Dec 21st, 2006
 

As i said earlier i've also seen this method ( getInstance() ) in the Calender class also. ( java.util.Calender) Like this way U can find it in some more classes. Ultimate answer is ,it returns an object of the class. where ever it is, the purpose of the method is remains same. Nagu , i hope u got my point.

  Was this answer useful?  Yes

abhysg

  • Dec 27th, 2006
 

How can we create objects using "deserialization" .. could you pls give piece of code..

  Was this answer useful?  Yes

shoaeb uddin

  • Feb 15th, 2007
 

Hi,I request the users to write the code snippet,I will write from 2mr onwards.
1.By using new operator
2.By using factory method
3.Using cloning technique
4.By using getInstance();
5.By Using class.forName("classname").newInastance();

  Was this answer useful?  Yes

abhijeet

  • Jan 18th, 2011
 

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

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

  Was this answer useful?  Yes

HI nagu,
Most of the classes is having getInstance methods. by calling this method we are getting the objects of that class.

Example:  Calendar c=Calendar.getInstance();

Here we are not using new, even though we are getting Calendar reference C and able to invoke calendar methods.

Note: Above is as per my knowledge.

  Was this answer useful?  Yes

janardhan kolla

  • Oct 27th, 2011
 

The object can be created by using four ways.They are:

(1). By using NEW operator

(2). By using class.forName("objectName")

(3) Using factory methods:

NumberFormat obj=NumberFormat.getNumberInstance()


(4) By using clone() method

Employee obj1=new Employee();
Employee obj2=(Employee)obj1.clone()


  Was this answer useful?  Yes

MasterAnand@java

  • Nov 13th, 2011
 

Hi friends

All above examples are true

There is one more way to create an object or instance

While writing JEE app servlet object or instance gets created when

1) First request comes
2) tag in tag in Web.xml file

Hence we can create object this way also

  Was this answer useful?  Yes

prabhul

  • Nov 21st, 2011
 

There are four different ways to create objects in java:

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.

MyObject object = new MyObject();

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.

MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();

3. Using clone() The clone() can be used to create a copy of an existing object.

MyObject anotherObject = new MyObject();
MyObject object = anotherObject.clone();

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();

  Was this answer useful?  Yes

manju4u

  • Nov 25th, 2011
 

Everybody here has taken getInstance() method in wrong way. its not one of the way to create object in java.

getInstance() is static method and intern uses new operator to create the object. Java provides these kind of methods just to abstract the object creation and enforce to use the getInstance() by making the constructor private.

  Was this answer useful?  Yes

Ghouse

  • Mar 29th, 2012
 

In 5 ways

1. New operator
2. New instance
3. Clone
4 Factory method
5 De-Serialization

  Was this answer useful?  Yes

Sreenivasulu B

  • May 17th, 2012
 

We can create an object in java by six ways.......

1.new operator
2.class.forName
3.Factory Method
4.Cloning
5.Deserialization
6.Reflection API

  Was this answer useful?  Yes

chandrasekhar rao naidu

  • Feb 8th, 2013
 

3 ways

1. new Operator,
2.object class clone() method
3.newInstance()
these are main.
and another ways also there
1.class.forName()---------> this method internally uses the newInstance method,
2.Deserialization-----> this is also internally use the above 3 ways

  Was this answer useful?  Yes

Ashish

  • Jun 12th, 2014
 

There are 7 ways to create object...

1 Using : new keyword

2 Using : Static Factory

3 Using : Instance Factory

4 Using : Factory Pattern

5 Using : De-serialization

6 Using : Clone Method

7 Using : new Instance method


Code
  1. import java.sql.Date;

  2.  

  3. public class Practice {

  4.         public static void main(String[] args)

  5.         {

  6.  

  7.         try{

  8.         int a=10;

  9.         int b=0;

  10.         int c=a/b;

  11.         }catch(Exception e)

  12.         {

  13.                 System.out.println(e.toString());

  14.                 System.err.println(e.toString());

  15.                 //e.printStackTrace();

  16.                 //System.out.println(e.getMessage());

  17.  

  18.         }

  19.         }

  20. }

  21.  

  22. =====================================================================================

  23. import java.io.Serializable;

  24.  

  25.  

  26. public class Student implements Serializable{

  27.         int Sno;

  28.         String Sname;

  29.         transient float avg;

  30.         public Student() {

  31.                 System.out.println("Default const called");

  32.         }

  33.        

  34.         public Student(int x, String y, float z) {

  35.                 Sno = x;

  36.                 Sname =y;

  37.                 avg =z;

  38.                 System.out.println("Parameterized const called");

  39.         }

  40.        

  41.         public void Display() {

  42.                 System.out.println("S No = "+Sno + "Name = "+Sname+" avg = "+avg);

  43.         }

  44.  

  45. }

  46. =====================================================================================

  47. public class CloneExample implements Cloneable{

  48.         int a;

  49.         float b;

  50.         public CloneExample() {

  51.                 System.out.println("Default const..");

  52.         }

  53.         public CloneExample(int a, float b) {

  54.                 super();

  55.                 this.a = a;

  56.                 this.b = b;

  57.                 System.out.println("Parameterized const..");

  58.         }

  59.        

  60.         public String toString() {

  61.                 return "a ="+a+"b ="+b;

  62.         }

  63.        

  64.         public Object MyClone() throws CloneNotSupportedException {

  65.                 return super.clone();

  66.         }

  67.  

  68.         public static void main(String[] args) throws CloneNotSupportedException {

  69.                 CloneExample ce = new CloneExample(100,25.54f);

  70.                 System.out.println("ce obj data "+ce.toString());

  71.                 System.out.println(ce.hashCode());

  72.                 // create obj through clone method

  73.                

  74.                 CloneExample ce1 = (CloneExample)ce.MyClone();

  75.                 System.out.println("ce1 obj data "+ce1.toString());

  76.                 System.out.println(ce1.hashCode());

  77.  

  78.                 ce.a = 105;

  79.                 System.out.println("ce obj data "+ce.toString());

  80.                 System.out.println("ce1 obj data "+ce1.toString());

  81.  

  82.                 ce1.b = 121.56f;

  83.                 System.out.println("ce obj data "+ce.toString());

  84.                 System.out.println("ce1 obj data "+ce1.toString());

  85.         }

  86. }

  87. ===============================================================================

  88. import java.io.FileInputStream;

  89. import java.io.IOException;

  90. import java.io.ObjectInputStream;

  91.  

  92.  

  93. public class ReadData {

  94.  

  95.         public static void main(String[] args) throws ClassNotFoundException {

  96.                 try {

  97.                         FileInputStream fis = new FileInputStream("abc.txt");

  98.                         ObjectInputStream ois = new ObjectInputStream(fis);

  99.                         // Deserial....

  100.                         Student s = (Student)ois.readObject();

  101.                         System.out.println(s.Sno+" "+s.Sname+" "+s.avg);

  102.                 }

  103.                 catch(IOException ie) {

  104.                         ie.printStackTrace();

  105.                 }

  106.  

  107.         }

  108.  

  109. }

  110. ===============================================================================

  111. import java.awt.Button;

  112. import java.io.FileInputStream;

  113. import java.io.IOException;

  114. import java.io.ObjectInputStream;

  115.  

  116.  

  117. public class ObjEx {

  118.         public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {

  119.                 // First : Using new key-word

  120.                 Object obj = new Object();

  121.                 System.out.println("First "+obj.hashCode());

  122.                

  123.                 // Second : Using Static Factory

  124.                 Thread t =  Thread.currentThread();

  125.                 System.out.println("Second "+t.hashCode());

  126.                

  127.                 // Third : Using Instance Factory

  128.                 String s1 = new String("Suraj");

  129.                 String s2 = s1.concat("Chaudhary");

  130.                 System.out.println("Third "+s2.hashCode());

  131.                

  132.                 //Fourth : factory Pattern

  133.                 StringBuffer sb = new StringBuffer("Hello World");

  134.                 String str = sb.substring(2,4);

  135.                 System.out.println("Fourth "+str.hashCode());

  136.                

  137.                 //Fifth : Deserial...

  138.                 try {

  139.                         FileInputStream fis = new FileInputStream("abc.txt");

  140.                         ObjectInputStream ois = new ObjectInputStream(fis);

  141.                         Student s = (Student)ois.readObject();

  142.                         System.out.println(s.Sno+" "+s.Sname+" "+s.avg);

  143.                         System.out.println("Fifth "+s.hashCode());

  144.                 }

  145.                 catch(IOException ie) {

  146.                         ie.printStackTrace();

  147.                 }

  148.                

  149.                 // Six : new Instance method

  150.                

  151.                 Class c = Class.forName("java.awt.Button");

  152.                 Button b = (Button)c.newInstance();

  153.                 System.out.println("Six "+b.hashCode());

  154.         }

  155. }

  156. ==================================================================================

  157. import java.io.FileOutputStream;

  158. import java.io.IOException;

  159. import java.io.ObjectOutputStream;

  160.  

  161.  

  162. public class Test {

  163.  

  164.         public static void main(String[] args) {

  165.                 try{

  166.                         FileOutputStream fos = new FileOutputStream("abc.txt");

  167.                         ObjectOutputStream oos = new ObjectOutputStream(fos);

  168.                         //Perform Serealization....

  169.                         Student s1 = new Student(11,"Suraj",35.78f);

  170.                         oos.writeObject(s1);

  171.                         fos.close();

  172.                         oos.close();

  173.                 }

  174.                 catch(IOException ie) {

  175.                         ie.printStackTrace();

  176.                 }

  177.         }

  178. }

  179.  

  Was this answer useful?  Yes

Prashant Shingne

  • Sep 9th, 2016
 

There are 4 ways to create object
1.New Operator Employee emp = new Employee();
2.Object.clone()
3.class.forName()
4.Deserilization readObject();

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions