- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are all the ways an object can be created in Java?
You can create an object
Using new keyword.
Sample obj = new Sample();
- Using the newInstance() method and Class.forName() method.
Sample obj2 = (Sample) Class.forName("Sample").newInstance();
- Using the clone() method by implementing Cloneable Interface (marker).
Sample obj3 = (Sample) obj1.clone();
- Using class loader.
Object obj4 = Sample.class.getClassLoader().loadClass("Sample");
- Using the constructor class from lang.reflect.
Class cls = Sample.class; Constructor obj = cls.getDeclaredConstructors()[0]; Sample obj5 = (Sample) obj.newInstance();
Advertisements