As we know, an object is a real-world entity that represents an instance of a class. This is a common interview question for freshers and Java developers with up to two years of experience. Understanding the different ways to create an object in Java is essential for interview preparation.
This is the most common way to create an object in Java. Almost 99% of objects are created using this approach.
e.g Employee emp=new Employee();
Output:- Object Creation Example
If we know the name of the class and it has a public no-argument (default) constructor, we can create an object using this approach. The
There are four ways to create an object in Java. In this post, we will discuss each method in detail along with practical examples.
1) Using new keyword
This is the most common way to create an object in Java. Almost 99% of objects are created using this approach.
e.g Employee emp=new Employee();
package com.adnjavainterview;
public class Employee {
public String getName() {
System.out.println("Object Creation Example");
}
public static void main(String[] args) {
Employee emp = new Employee();
emp.getName();
}
}
Output:- Object Creation Example
2) Using Class.forName()
If we know the name of the class and it has a public no-argument (default) constructor, we can create an object using this approach. The
Class.forName() method loads the class into memory, and the newInstance() method of the Class object is then used to create an instance of the class.Note for Java 9 and later
If your blog targets modern Java versions (Java 9+ or Java 17), it's worth mentioning that Class.newInstance() has been deprecated.
Employee obj =(Employee) Class..forName("com.adn.Employee").newInstance();
package com.adnjavainterview;
public class Employee {
public String getName() {
System.out.println("Object Creation Example");
}
public static void main(String[] args) {
Employee emp = Class.forName("Employee").newInstance();
emp.getName();
}
}
Output:- Object Creation Example
3) Using clone()
The
clone() method is used to create a copy of an existing object. It copies the contents of the original object into a newly created object. To use the clone() method, the class must implement the Cloneable interface.Employee emp=new Employee();
Employee cloneObject=emp.clone();
public class Employee implements Cloneable {
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public String getName() {
System.out.println("Object Creation Example");
}
public static void main(String[] args) {
Employee emp = new Employee();
try {
Employee cloneObject = (Employee) emp.clone();
System.out.println(cloneObject.getName());
}
catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
4) Using object deserialization
Object deserialization is the process of creating an object from its serialized form.
ObjectInputStream inStream = new ObjectInputStream(anInputStream);
MyObject object = (MyObject) inStream.readObject();
Related post:
1) Java Object Class and its methods
2) What is abstract class and interface? Difference between them.
3) What are the immutable classes in Java? How to create immutable class and What are the conditions?
4) String Interview Questions and Answers
Good one
ReplyDeleteThanks Narender. Keep on visiting blog. If you have any queries please comment it.
ReplyDeleteguyiyaan
ReplyDeleteawesome content
ReplyDelete