Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are all the different ways to create an object in Java?

Tags:

java

There are four different ways to create objects in java:

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

You can read them from here.


There are various ways:

  • Through Class.newInstance.
  • Through Constructor.newInstance.
  • Through deserialisation (uses the no-args constructor of the most derived non-serialisable base class).
  • Through Object.clone (does not call a constructor).
  • Through JNI (should call a constructor).
  • Through any other method that calls a new for you.
  • I guess you could describe class loading as creating new objects (such as interned Strings).
  • A literal array as part of the initialisation in a declaration (no constructor for arrays).
  • The array in a "varargs" (...) method call (no constructor for arrays).
  • Non-compile time constant string concatenation (happens to produce at least four objects, on a typical implementation).
  • Causing an exception to be created and thrown by the runtime. For instance throw null; or "".toCharArray()[0].
  • Oh, and boxing of primitives (unless cached), of course.
  • JDK8 should have lambdas (essentially concise anonymous inner classes), which are implicitly converted to objects.
  • For completeness (and Paŭlo Ebermann), there's some syntax with the new keyword as well.

Within the Java language, the only way to create an object is by calling its constructor, be it explicitly or implicitly. Using reflection results in a call to the constructor method, deserialization uses reflection to call the constructor, factory methods wrap the call to the constructor to abstract the actual construction and cloning is similarly a wrapped constructor call.


Yes, you can create objects using reflection. For example, String.class.newInstance() will give you a new empty String object.


There are five different ways to create an object in Java,

1. Using new keyword → constructor get called

Employee emp1 = new Employee();

2. Using newInstance() method of Class → constructor get called

Employee emp2 = (Employee) Class.forName("org.programming.mitra.exercises.Employee")
                                .newInstance();

It can also be written as

Employee emp2 = Employee.class.newInstance();

3. Using newInstance() method of Constructor → constructor get called

Constructor<Employee> constructor = Employee.class.getConstructor();
Employee emp3 = constructor.newInstance();

4. Using clone() method → no constructor call

Employee emp4 = (Employee) emp3.clone();

5. Using deserialization → no constructor call

ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"));
Employee emp5 = (Employee) in.readObject();

First three methods new keyword and both newInstance() include a constructor call but later two clone and deserialization methods create objects without calling the constructor.

All above methods have different bytecode associated with them, Read Different ways to create objects in Java with Example for examples and more detailed description e.g. bytecode conversion of all these methods.

However one can argue that creating an array or string object is also a way of creating the object but these things are more specific to some classes only and handled directly by JVM, while we can create an object of any class by using these 5 ways.