How many ways to create objects in java
There are many ways to create an object in java. They are:
- By new keyword
- By newInstance() method
- By clone() method
- By deserialization
- By factory method
A. 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();
B. 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();
C. Using clone()
The clone() can be used to create a copy of an existing object.
MyObject anotherObject = new MyObject();
MyObject object = (MyObject) anotherObject.clone();
D. 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();
METHOD 1: By new keyword
Using new keyword. This is the most common way to create an object in java. Almost 99% developer create object in this way.
Object obj = new Object();
METHOD 2: By newInstance() method
Using Class.forName(). Class.forName()
gives you the class object, which is useful for reflection. The methods that this object has are defined by Java, not by the programmer writing the class. They are the same for every class. Calling newInstance() on that gives you an instance of that class (i.e. callingClass.forName("ExampleClass").newInstance() it is equivalent to calling new ExampleClass()), on which you can call the methods that the class defines, access the visible fields etc.
Employee object2 = (Employee)Class.forName("com.com.example.Employee").newInstance();
Class.forName() will always use the ClassLoader of the caller, whereas ClassLoader.loadClass() can specify a different ClassLoader. I believe that Class.forName initializes the loaded class as well, whereas the ClassLoader.loadClass() approach doesn’t do that right away (it’s not initialized until it’s used for the first time).
METHOD 3 : By clone() method
Using clone(). The Object::clone()
can be used to create a copy of an existing object.
Employee object= new Employee ();
Employee object1= (Employee) object.clone();
METHOD 4: By deserialization
Using Class::newInstance()
method. See Oracle Tutorial.
Object object= Employee.class.getClassLoader().loadClass("com.tech.example.Employee").newInstance();
METHOD 5: By factory method
Using Object Deserialization. Object Deserialization is nothing but creating an object from its serialized form.
// create a new file with an ObjectOutputStream
FileOutputStream outStream = new FileOutputStream("employee.txt");
ObjectOutputStream outStream1= new ObjectOutputStream(outStream);
// write something in the file
outStream1.writeObject(object);
outStream1.flush();
// create an ObjectInputStream for the file we created before
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("employee.txt"));
Employee object = (CrunchifyObj) ois.readObject();
METHOD 6:
Use the Constructor
class from the java.lang.reflect package, part of Java Reflection facility.
Class clazz = Employee.class;
Constructor empCon= clazz.getDeclaredConstructors()[0];
Employeeobj = (Employee) empCon.newInstance();
post a comment