https://grokonez.com/java/ways-to-create-a-java-object
Ways to create a Java Object
Object is an important concept in Java Development. This tutorial shows you the ways to create a Java Object.
MyJavaClass object = new MyJavaClass();
new MyJavaClass()
, we can call newInstance()
method in this way:
MyJavaClass object = (MyJavaClass) Class.forName("com.javasampleapproach.createobject.MyJavaClass").newInstance();
newInstance()
method which can be used to create objects.
Constructor constructor = MyJavaClass.class.getConstructor();
MyJavaClass object = constructor.newInstance();
clone()
method, the JVM generates exactly the object which content is the same as content of the object which call the method.
// we should implement Cloneable and define the clone() method before
MyJavaClass object2 = (MyJavaClass) object1.clone();
More at:
https://grokonez.com/java/ways-to-create-a-java-object
Ways to create a Java Object
#java #object