
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Different Ways to Create an Object in Java
In Java a class is a user defined datatype/blue print in which we declare methods and variables.
public class Sample{ }
Once you declare a class you need to create an object (instantiate) it. You can create an object using various ways −
Using new keyword
In general, an object is created using the new keyword as −
Sample obj = new Sample();
Example
In the following Java we have a class with name sample, which has a method (display). From the main method we are instantiating the Sample class and invoking the display() method.
public class Sample{ public void display() { System.out.println("This is the display method of the Sample class"); } public static void main(String[] args){ //Instantiating the Sample class Sample obj = new Sample(); obj.display(); } }
Output
This is the display method of the Sample class
Using the newInstance() method
The newInstance() method of the class named Class creates an object of the class represented by this Class object.
You can get the Class object of a class by passing its name as String to the forName() method.
Sample obj2 = (Sample) Class.forName("Sample").newInstance();
Example
In the following Java we have a class with name sample, which has a method (display). From the main method we are creating an object of the Sample class using the newInstance() method of the class Class.
public class Sample{ public void display() { System.out.println("This is the display method of the Sample class"); } public static void main(String[] args) throws Exception{ //Creating the Class object of Sample class Class cls = Class.forName("Sample"); Sample obj = (Sample) cls.newInstance(); obj.display(); } }
Output
This is the display method of the Sample class
Using the clone() method
The clone() of the Object class creates and returns an object of the current class.
Example
In the following Java we have a class with name sample, which has a method (display). From the main method we are creating an object of the Sample class and from it we are creating another object using the clone() method.
public class Sample{ public void display() { System.out.println("This is the display method of the Sample class"); } public static void main(String[] args) throws Exception{ //Creating the Class object of Sample class Sample obj1 = new Sample(); //Creating another object using the clone() method Sample obj2 = (Sample) obj1.clone(); obj2.display(); } }
Output
This is the display method of the Sample class
Using the constructor class from lang.reflect
The newInstance() method of the java.lang.reflect.Constructor class creates and returns a new object using the current constructor.
Example
In the following Java we have a class with name sample, which has a method (display). From the main method we are creating an object of the Sample class using the java.lang.reflect.Constructor class.
import java.lang.reflect.Constructor; public class Sample { public void display() { System.out.println("This is the display method of the Sample class"); } public static void main(String[] args) throws Exception{ //Creating a Constructor class Constructor constructor = Sample.class.getDeclaredConstructor(); //Creating an object using the newInstance() method Sample obj = constructor.newInstance(); obj.display(); } }
Output
This is the display method of the Sample class