Five Different Ways to Create Objects in Java

a thread...

1. Using a new keyword.

It's the most popular one. We create an object by using a new operator followed by a constructor call.

Eg:
2. Using the newInstance() method of class "Class":

Eg:
3️. Using the newInstance() method in class "Constructor":

Eg:
Note:

Both the above ways (Shown in 2 and 3),
are known as reflective ways of creating objects.

Fun-fact:

Class's newInstance() method internally uses Constructor's newInstance() method.
4️. Using "Object" class clone() method

The clone() method creates a copy of an existing object.

The clone() method is part of the "Object" class which returns a clone object.

Eg:
When using the clone() method:

Always Remember,

1. The "Cloneable" interface is implemented.
2. The clone() method must be overridden with other classes.
3. Inside the clone() method, the class must call super.clone().
5️. Using Deserialization:

When we deserialize any object then JVM creates a new object internally.

For this, we need to implement the Serializable interface.

Eg:
Conclusion:

We've seen all five ways to create Objects in Java:

1. Using a new keyword.
2. Using Class.newInstance() method
3️. Using Constructor.newInstance() method
4️. Using Object.clone() method
5️. Using Deserialization

Thanks for reading!

More from Vikas Rajput

You May Also Like

The YouTube algorithm that I helped build in 2011 still recommends the flat earth theory by the *hundreds of millions*. This investigation by @RawStory shows some of the real-life consequences of this badly designed AI.


This spring at SxSW, @SusanWojcicki promised "Wikipedia snippets" on debated videos. But they didn't put them on flat earth videos, and instead @YouTube is promoting merchandising such as "NASA lies - Never Trust a Snake". 2/


A few example of flat earth videos that were promoted by YouTube #today:
https://t.co/TumQiX2tlj 3/

https://t.co/uAORIJ5BYX 4/

https://t.co/yOGZ0pLfHG 5/
Great article from @AsheSchow. I lived thru the 'Satanic Panic' of the 1980's/early 1990's asking myself "Has eveyrbody lost their GODDAMN MINDS?!"


The 3 big things that made the 1980's/early 1990's surreal for me.

1) Satanic Panic - satanism in the day cares ahhhh!

2) "Repressed memory" syndrome

3) Facilitated Communication [FC]

All 3 led to massive abuse.

"Therapists" -and I use the term to describe these quacks loosely - would hypnotize people & convince they they were 'reliving' past memories of Mom & Dad killing babies in Satanic rituals in the basement while they were growing up.

Other 'therapists' would badger kids until they invented stories about watching alligators eat babies dropped into a lake from a hot air balloon. Kids would deny anything happened for hours until the therapist 'broke through' and 'found' the 'truth'.

FC was a movement that started with the claim severely handicapped individuals were able to 'type' legible sentences & communicate if a 'helper' guided their hands over a keyboard.