- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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(); Sample obj5 = (Sample) obj.newInstance();
- Related Articles
- How many ways a String object can be created in java?
- What are all the ways keyword ‘this’ can be used in Java?
- Can a final variable be initialized when an object is created in Java?
- What are the ways in which a thread is created in Java?
- What are the different ways the transaction can be executed(DBMS)?
- 3 ways to initialize an object in Java
- Different ways to create an object in java?
- Can energy be destroyed? Can energy be created ?
- How many types of JDialog boxes can be created in Java?
- What are the different ways to print an exception message in java?
- What are the different ways to iterate over an array in Java?
- Do all properties of an Immutable Object need to be final in Java?
- How an iterator object can be used to iterate a list in Java?
- State three ways in which the strength of an electromagnet can be increased.