

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
5 different ways to create objects in Java
Consider a class Tester which has implemented Cloneable interface. Now you can initialize an object using following five ways:
1. Using new keyword.
Tester tester1 = new Tester();
2. Using Class.forName() method
Tester tester2 = (Tester)Class.forName("Tester").newInstance();
3. Using clone method.
Tester tester3 = tester1.clone();
4. Using Constructor.forName() method
Tester tester4 = Tester.class.getConstructor().newInstance();
5. Using Deserialization
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream ); Tester tester5 = (MyObject) objectInputStream.readObject();
Using new keyword is the most preferred one.
- Related Questions & Answers
- Different ways to create Objects in Java
- Different ways to create an object in java?
- Extending a list in Python (5 different ways)
- What are the different ways to create an object in Java
- Different ways to concatenate Strings in Java
- Different ways to print exception messages in Java
- Different ways to overload a method in Java
- Different ways to traverse an Array in Java?
- Different ways to format long with Java System.out.format
- Different ways for Integer to String conversion in Java
- Java Program to display 5 different cards in a CardLayout
- Different Ways to Add Parentheses in C++
- Create Objects of a Class in Java
- What are the different ways to iterate over an array in Java?
- What are the different ways to print an exception message in java?
Advertisements