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,880 points
101 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();
answered Oct 16, 2018 by Daisy
• 8,050 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 by Sirajul
• 40,990 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 in Java by Sirajul
• 40,990 points
237 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 in Java by Wasim
71 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,350 points
69 views
0 votes
1 answer

What is the concept of Immutability for strings in Java ? Why are strings immutable ?

According to Effective Java, chapter 4, page 73, ...READ MORE

answered May 11, 2018 in Java by Rishabh
• 3,540 points
205 views
+5 votes
3 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,380 points

edited Nov 6, 2018 by Omkar 11,298 views
0 votes
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,110 points
2,417 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,110 points
6,255 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,110 points
779 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,190 points
705 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,050 points
263 views