• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Different ways of creating object in java

December 13, 2011 By j2eereference 2 Comments

There are different ways to create objects in java:

1) Using new keyword

This is the most common way to create an object in java.

MyObject object = new MyObject();

2) Using Class.forName()

If we know the name of the class & if it has a public default constructor we can create an object in this way. It is also known as reflection.

 

MyObject object = (MyObject)Class.forName(“com.abc.MyObject”). newInstance();

3) Using clone()

The clone() can be used to create a copy of an existing object.

 MyObject anotherObject = new MyObject();

 MyObject object = anotherObject.clone();

4) Using object deserialization

Object deserialization is nothing but creating an object from its serialized form.

ObjectInputStream inStream = new ObjectInputStream(anInputStream );

MyObject object = (MyObject) inStream.readObject();

5) Using reflection in another way.

this.getClass().getClassLoader().loadClass(“com.abc.myobject”).newInstance();

Related Posts

  • UncaughtExceptionHandler in java
  • How to generate and resolve OutOfMemoryError?
  • Difference between Spring’s Singleton scope & Singleton Design pattern
  • How to stop a thread?
  • Interrupting a thread in java
  • What is ThreadLocal in Java ?
  • ArrayList custom Implementation
  • Difference between volatile and synchronized keyword?
  • How to write thread safe code?
  • How to avoid deadlock in java ?

Filed Under: Core Java

Reader Interactions

Comments

  1. skz says

    July 30, 2012 at 6:43 pm

    Its really great :), was aware of few ways but you presented 5 ways .. thats great. Keep up the good work

    Reply
  2. RandomGuy says

    July 10, 2013 at 2:01 pm

    BTW the 5th way is also using newInstance() method…which is not too different from 2…
    Please tell about factory and factory-pattern methods.

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.