Different ways to create objects in Java

Different approaches to create an Object.

We will look and study 5 approaches which can be used for object creation. So let’s begin.

Method #1 – Using new keyword

This is the most common approach the developers use in Java to create an Object. I read somewhere that this approach is also called static instance creation.

GoyalsBit obj = new GoyalsBit();

Method #2- Using Class.forName ()

If we know the name of the class then this method is useful but point to be noted here is that the class should have a public constructor.  Now doesn’t it sound familiar!! Yes if you have worked in JDBC then we use this method to connect to DB. Class.forName actually loads the class in Java but doesn’t create any Object. To Create an Object of the Class you have to use newInstance method. Calling Class.forName(“NameofClass”).newInstance() is equivalent to declaring object using new keyword.

GoyalsBit obj=(GoyalsBit)Class.forName(“com.test.GoyalsBit”).newInstance();

Method #3- Using clone ()

This method can be used to create a copy of an existing object. Object.clone() is a native method which translates into instructions for allocating the memory and copying the data

GoyalsBit obj = new GoyalsBit ();

GoyalsBit object1 = (GoyalsBit) obj.clone();

As I have already marked in bold that using this technique we are only creating clone of an existing object and not new object. Class need to implement Cloneable Interface otherwise it will throw CloneNotSupportedException.

Method #4- Using Deserialization ()

Deserialization is the process of creating the new object on the remote machine from its serialize form.

ObjectInputStream inStream =new ObjectInputStream(anInputStream );

GoyalsBit object (GoyalsBit) inStream.readObject();

Method #5- Using ClassLoader()

We can also use Class Loader to create Object of a Class. This way is somewhat same as Class.forName option.

Object obj = GoyalsBit.class.getClassLoader().loadClass(“com.test.GoyalsBit “).newInstance();

I have shared the different ways to create an object in Java. In case there are some more, then please share them in comments section.

Reference

To view or add a comment, sign in

More articles by Krishna Sankar Palanisami

  • Book on "Human Computer Interaction"

    This book “Human Computer Interaction”, covering various design technologies for folks and person with disabilities. It…

    3 Comments
  • Book on "Knowledge Management"

    This book “Knowledge Management” is almost provides a detailed outline towards evolution of Knowledge Management with…

    1 Comment
  • Book on “Multi-Core Architectures and Programming”

    This book “Multi-Core Architectures and Programming” is about an introductory conceptual idea about Multicore Processor…

  • Book on “Object Oriented Programming”

    This book “Object Oriented Programming” is about basic idea towards object oriented programming with C++ and Java. It…

  • Book on “Foundation Skills in Integrated Product Development”

    This book “Foundation Skills in Integrated Product Development” is about an exploratory awareness to play as role of…

    1 Comment
  • Book on Object Oriented Analysis and Design

    This book “Object Oriented Analysis and Design” is about an introductory idea on pattern design and implement the…

  • Book on Grid and Cloud Computing ...

    This book “Grid and Cloud Computing” is about an exploratory awareness to solve large scale scientific problems through…

    2 Comments
  • My Book on Artificial Intelligence ...

    This book “Artificial Intelligence” is about an introductory idea on machines used in real world trained with…

    5 Comments

Explore content categories