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

0 votes
There's the obvious which is to use a constructor, but what other ways are there?
Oct 16, 2018 in Java by Sushmita
• 6,920 points
1,157 views

2 answers to this question.

0 votes

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


Hope this helps!

Check out Java course to learn more about it.

Thanks!

answered Oct 16, 2018 by Daisy
• 8,140 points
0 votes

There are different ways you could do this :

  • Using Class.newInstance.

  • Using Constructor.newInstance.

  • Using deserialisation (uses the no-args constructor of the most derived non-serialisable base class).

  • Using Object.clone (does not call a constructor).

  • Using JNI (should call a constructor).

  • Through any other method that calls a new.

  • 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.

  • There's some syntax with the new keyword also.

answered Aug 19, 2019 by Sirajul
• 59,230 points

Related Questions In Java

0 votes
2 answers

In Java, what is the best way to determine the size of an object?

I happened to find a java class "jdk.nashorn.internal.ir.debug.ObjectSizeCalculator", ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,230 points
13,067 views
0 votes
1 answer

What are the different ways of comparing Strings in Java?

The different ways of comparing string in ...READ MORE

answered Mar 5, 2019 in Java by Wasim
983 views
0 votes
1 answer

How many ways to create object in java?

In Java, there are several methodologies to ...READ MORE

answered Nov 9, 2023 in Java by anonymous
• 3,360 points
409 views
0 votes
1 answer

what are the ways in which a list can be iterated

  There are 5 ways to iterate over ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,370 points
1,101 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 81,220 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
8,357 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
17,432 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points

edited Aug 4, 2023 by Khan Sarfaraz 4,874 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,220 points
3,576 views
0 votes
2 answers

What's the best way to check if a String represents an integer in Java?

You can also use regular expression. str.matches("-?\\d+"); It will ...READ MORE

answered Aug 9, 2018 in Java by Daisy
• 8,140 points
3,954 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP