Had a conversation with a coworker the other day about this.
There's the obvious which is to use a constructor, but what other ways are there?
Had a conversation with a coworker the other day about this.
There's the obvious which is to use a constructor, but what other ways are there?
We can also create the object in this way:-
Nobody has discuss it.
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.
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
There are various ways:
Class.newInstance
.Constructor.newInstance
.Object.clone
(does not call a constructor).new
for you.String
s)....
) method call (no constructor for arrays).throw null;
or"".toCharArray()[0]
.new
keyword as well.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.