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

#தினம்_ஒரு_திருவாசகம்
தொல்லை இரும்பிறவிச் சூழும் தளை நீக்கி
அல்லல் அறுத்து ஆனந்தம் ஆக்கியதே – எல்லை
மருவா நெறியளிக்கும் வாதவூர் எங்கோன்
திருவாசகம் என்னும் தேன்

பொருள்:
1.எப்போது ஆரம்பித்தது என அறியப்படமுடியாத தொலை காலமாக (தொல்லை)

2. இருந்து வரும் (இரும்)


3.பிறவிப் பயணத்திலே ஆழ்த்துகின்ற (பிறவி சூழும்)

4.அறியாமையாகிய இடரை (தளை)

5.அகற்றி (நீக்கி),

6.அதன் விளைவால் சுகதுக்கமெனும் துயரங்கள் விலக (அல்லல் அறுத்து),

7.முழுநிறைவாய்த் தன்னுளே இறைவனை உணர்த்துவதே (ஆனந்த மாக்கியதே),

8.பிறந்து இறக்கும் காலவெளிகளில் (எல்லை)

9.பிணைக்காமல் (மருவா)

10.காக்கும் மெய்யறிவினைத் தருகின்ற (நெறியளிக்கும்),

11.என் தலைவனான மாணிக்க வாசகரின் (வாதவூரெங்கோன்)

12.திருவாசகம் எனும் தேன் (திருவா சகமென்னுந் தேன்)

முதல்வரி: பிறவி என்பது முன்வினை விதையால் முளைப்பதோர் பெருமரம். அந்த ‘முன்வினை’ எங்கு ஆரம்பித்தது எனச் சொல்ல இயலாது. ஆனால் ‘அறியாமை’ ஒன்றே ஆசைக்கும்,, அச்சத்துக்கும் காரணம் என்பதால், அவையே வினைகளை விளைவிப்பன என்பதால், தொடர்ந்து வரும் பிறவிகளுக்கு, ‘அறியாமையே’ காரணம்

அறியாமைக்கு ஆரம்பம் கிடையாது. நமக்கு ஒரு பொருளைப் பற்றிய அறிவு எப்போதிருந்து இல்லை? அதைச் சொல்ல முடியாது. அதனாலேதான் முதலடியில், ஆரம்பமில்லாத அஞ்ஞானத்தை பிறவிகளுக்குக் காரணமாகச் சொல்லியது. ஆனால் அறியாமை, அறிவின் எழுச்சியால், அப்போதே முடிந்து விடும்.
I'm going to do two history threads on Ethiopia, one on its ancient history, one on its modern story (1800 to today). 🇪🇹

I'll begin with the ancient history ... and it goes way back. Because modern humans - and before that, the ancestors of humans - almost certainly originated in Ethiopia. 🇪🇹 (sub-thread):


The first likely historical reference to Ethiopia is ancient Egyptian records of trade expeditions to the "Land of Punt" in search of gold, ebony, ivory, incense, and wild animals, starting in c 2500 BC 🇪🇹


Ethiopians themselves believe that the Queen of Sheba, who visited Israel's King Solomon in the Bible (c 950 BC), came from Ethiopia (not Yemen, as others believe). Here she is meeting Solomon in a stain-glassed window in Addis Ababa's Holy Trinity Church. 🇪🇹


References to the Queen of Sheba are everywhere in Ethiopia. The national airline's frequent flier miles are even called "ShebaMiles". 🇪🇹
🌺श्री गरुड़ पुराण - संक्षिप्त वर्णन🌺

हिन्दु धर्म के 18 पुराणों में से एक गरुड़ पुराण का हिन्दु धर्म में बड़ा महत्व है। गरुड़ पुराण में मृत्यु के बाद सद्गती की व्याख्या मिलती है। इस पुराण के अधिष्ठातृ देव भगवान विष्णु हैं, इसलिए ये वैष्णव पुराण है।


गरुड़ पुराण के अनुसार हमारे कर्मों का फल हमें हमारे जीवन-काल में तो मिलता ही है परंतु मृत्यु के बाद भी अच्छे बुरे कार्यों का उनके अनुसार फल मिलता है। इस कारण इस पुराण में निहित ज्ञान को प्राप्त करने के लिए घर के किसी सदस्य की मृत्यु के बाद का समय निर्धारित किया गया है...

..ताकि उस समय हम जीवन-मरण से जुड़े सभी सत्य जान सकें और मृत्यु के कारण बिछडने वाले सदस्य का दुख कम हो सके।
गरुड़ पुराण में विष्णु की भक्ति व अवतारों का विस्तार से उसी प्रकार वर्णन मिलता है जिस प्रकार भगवत पुराण में।आरम्भ में मनु से सृष्टि की उत्पत्ति,ध्रुव चरित्र की कथा मिलती है।


तदुपरांत सुर्य व चंद्र ग्रहों के मंत्र, शिव-पार्वती मंत्र,इन्द्र सम्बंधित मंत्र,सरस्वती मंत्र और नौ शक्तियों के बारे में विस्तार से बताया गया है।
इस पुराण में उन्नीस हज़ार श्लोक बताए जाते हैं और इसे दो भागों में कहा जाता है।
प्रथम भाग में विष्णुभक्ति और पूजा विधियों का उल्लेख है।

मृत्यु के उपरांत गरुड़ पुराण के श्रवण का प्रावधान है ।
पुराण के द्वितीय भाग में 'प्रेतकल्प' का विस्तार से वर्णन और नरकों में जीव के पड़ने का वृत्तांत मिलता है। मरने के बाद मनुष्य की क्या गति होती है, उसका किस प्रकार की योनियों में जन्म होता है, प्रेत योनि से मुक्ति के उपाय...