Knowledge Walls
John Peter
Pune, Maharashtra, India
What are the different ways to create an object in JAVA?
3334 Views
1. Create an object using "new" keyword
ClassName object = new ClassName();
5. Assign serialized object stream to object, which is written by writeObject()
ObjectInputStream ois = new ObjectInputStream(InputStream Object);
ClassName obj1 = (ClassName) ois.readObject();
2. Create instance using instance of a Class
ClassName obj1 = new ClassName();
ClassName obj2 = obj1.getClass().newInstance();
3. Cloning/Copying an object using clone() method
ClassName obj1 = new ClassName();
ClassName obj2 = obj1.clone();
4. Creating object using hardcoded Classname including package.
Object obj1 = Class.forName("com.google.Gson").newInstance();
Best Lessons of "One day One Thing to Know"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details