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

Viewed 440407

Had a conversation with a coworker the other day about this.

There's the obvious using a constructor, but what are the other ways there?

22 Answers

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.

Also you can use

 Object myObj = Class.forName("your.cClass").newInstance();

This should be noticed if you are new to java, every object has inherited from Object

protected native Object clone() throws CloneNotSupportedException;

Also, you can de-serialize data into an object. This doesn't go through the class Constructor !


UPDATED : Thanks Tom for pointing that out in your comment ! And Michael also experimented.

It goes through the constructor of the most derived non-serializable superclass.
And when that class has no no-args constructor, a InvalidClassException is thrown upon de-serialization.

Please see Tom's answer for a complete treatment of all cases ;-)
is there any other way of creating an object without using "new" keyword in java

Reflection will also do the job for you.

SomeClass anObj = SomeClass.class.newInstance();

is another way to create a new instance of a class. In this case, you will also need to handle the exceptions that might get thrown.

You can also clone existing object (if it implements Cloneable).

Foo fooClone = fooOriginal.clone (); 

From an API user perspective, another alternative to constructors are static factory methods (like BigInteger.valueOf()), though for the API author (and technically "for real") the objects are still created using a constructor.

Depends exactly what you mean by create but some other ones are:

  • Clone method
  • Deserialization
  • Reflection (Class.newInstance())
  • Reflection (Constructor object)

there is also ClassLoader.loadClass(string) but this is not often used.

and if you want to be a total lawyer about it, arrays are technically objects because of an array's .length property. so initializing an array creates an object.

Related