As you know, in Java, a class provides the blueprint for objects
; you create an object from a class.
There are four different ways to create objects in java:
Method-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.
CrunchifyObj object = new CrunchifyObj();
Method-2
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.
CrunchifyObj object2 = (CrunchifyObj) Class.forName("crunchify.com.tutorial.CrunchifyObj").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).
Another must read:
Method-3
Using clone()
. The clone() can be used to create a copy of an existing object.
CrunchifyObj secondObject = new CrunchifyObj(); CrunchifyObj object3 = (CrunchifyObj) secondObject.clone();
Method-4
Using newInstance()
method.
Object object4 = CrunchifyObj.class.getClassLoader().loadClass("crunchify.com.tutorial.CrunchifyObj").newInstance();
Method-5
Using Object Deserialization
. Object Deserialization is nothing but creating an object from its serialized form.
// Create Object5 // create a new file with an ObjectOutputStream FileOutputStream out = new FileOutputStream("crunchify.txt"); ObjectOutputStream oout = new ObjectOutputStream(out); // write something in the file oout.writeObject(object3); oout.flush(); // create an ObjectInputStream for the file we created before ObjectInputStream ois = new ObjectInputStream(new FileInputStream("crunchify.txt")); CrunchifyObj object5 = (CrunchifyObj) ois.readObject();
Method-6
use the Constructor class from the java.lang.reflect
Class clazz = CrunchifyObj.class; Constructor crunchifyCon = clazz.getDeclaredConstructors()[0]; CrunchifyObj obj = (CrunchifyObj) crunchifyCon.newInstance();
Complete Example:
package crunchify.com.tutorial; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.lang.reflect.Constructor; /** * @author Crunchify.com * Total 6 different way to create an Object in Java */ public class CrunchifyObj implements Cloneable, Serializable { private static final long serialVersionUID = 1L; public CrunchifyObj() { log("Hello! CrunchifyObj() just got created.."); } @Override protected Object clone() throws CloneNotSupportedException { return (CrunchifyObj) super.clone(); } @SuppressWarnings({ "unused", "resource", "rawtypes" }) public static void main(String[] args) throws Exception { // Create Object1 CrunchifyObj object1 = new CrunchifyObj(); // Create Object2 CrunchifyObj object2 = (CrunchifyObj) Class.forName("crunchify.com.tutorial.CrunchifyObj").newInstance(); // Create Object3 CrunchifyObj secondObject = new CrunchifyObj(); CrunchifyObj object3 = (CrunchifyObj) secondObject.clone(); // Create Object4 Object object4 = CrunchifyObj.class.getClassLoader().loadClass("crunchify.com.tutorial.CrunchifyObj") .newInstance(); // Create Object5: Create a new file with an ObjectOutputStream FileOutputStream out = new FileOutputStream("crunchify.txt"); ObjectOutputStream oout = new ObjectOutputStream(out); oout.writeObject(object4); // write object4 to file oout.flush(); // create an ObjectInputStream for the file we created before ObjectInputStream ois = new ObjectInputStream(new FileInputStream("crunchify.txt")); CrunchifyObj object5 = (CrunchifyObj) ois.readObject(); log(object5.toString()); // Create Object6: use the Constructor class from the java.lang.reflect Class clazz = CrunchifyObj.class; Constructor crunchifyCon = clazz.getDeclaredConstructors()[0]; CrunchifyObj obj = (CrunchifyObj) crunchifyCon.newInstance(); } private static void log(String object) { System.out.println(object); } }
Console Output:
Hello! CrunchifyObj() just got created.. Hello! CrunchifyObj() just got created.. Hello! CrunchifyObj() just got created.. Hello! CrunchifyObj() just got created.. crunchify.com.tutorial.CrunchifyObj@45ee12a7 Hello! CrunchifyObj() just got created..
Also, try opening crunchify.txt
file and you should see serialized object in that file.
The post What are all the Different Ways to Create an Object in Java? Total 6 ways – Complete Tutorial appeared first on Crunchify.
0 Commentaires