Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)
Can someone tell me what are the different ways to create an object in Java?

1 Answer

0 votes
by (13.1k points)

There are five ways to create an object in java:

  1. By using new keyword:

className object=new className();

  1. By using Class.forName() method:

className object=(className)Class.forName(“className”).newInstance();

  1. By using clone method:

className object =className.clone();

  1. By using Constructor.forName() method:

className object= className.class.getConstructor().newInstance();

 

  1. By using Deserialization:

ObjectInputStream ois=new ObjectInputStream(inputStream );

ClassName object=(MyObject) ois.readObject();

Want to learn Java? Check out the Java certification from Intellipaat.

Related questions

0 votes
1 answer
asked Feb 11, 2021 in Java by dante07 (13.1k points)
0 votes
0 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...