There are 5 main ways to create objects in Java:
1) Using the new keyword
2) Using new instance with Class.forName()
3) Using the clone() method for existing objects
4) Using deserialization to create objects from serialized files
5) Using the factory method pattern to abstract object creation
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Object Oriented Prograring(OOP) java
1.
2. 2
Quetion::explain the five way of
creating object in java
● Different ways to create objects in
Java
As you all know, in Java, a class
provides the blueprint for objects,
you create an object from a class.
There are many different ways to
create objects in Java.
Following are some ways in which
you can create objects in Java:
3. 3
1) Using new Keyword
● 1) Using new Keyword :
Using new keyword is the most basic
way to create an object. This is the
most common way to create an
object in java. Almost 99% of objects
are created in this way. By using this
method we can call any constructor
we want to call (no argument or
parameterized constructors).
5. 5
2) Using New Instance
2) Using New Instance :
● If we know the name of the class & if it has a
public default constructor we can create an
object –Class.forName. We can use it to
create the Object of a Class. 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 the new Instance
Method of the Class.
7. 7
3) Using clone() method
Using clone() method:
● Whenever clone() is called on any object,
the JVM actually creates a new object and
copies all content of the previous object
into it. Creating an object using the clone
method does not invoke any constructor.
● To use clone() method on an object we
need to implement Cloneable and define
the clone() method in it.
8. 8
.
Note :
1)Here we are creating the clone of an existing Object and not any new Object.
2)Class need to implement Cloneable Interface otherwise
it will throw CloneNotSupportedException.
9. 9
4) Using deserialization
4) Using deserialization :
● Whenever we serialize and then
deserialize an object, JVM creates a
separate object. In deserialization, JVM
doesn’t use any constructor to create the
object.
● To deserialize an object we need to
implement the Serializable interface in
the class.
11. 11
Deserialization of Object :
Object of DeserializationExample class is serialized
using writeObject() method and written to file.txt
file.
12. 12
5 Factory Method:
●
Factory method is a creational design pattern, i.e, related to object creation. In
Factory pattern, we create object without exposing the creation logic to client and the
client use the same common interface to create new type of object. The idea is to use
a static member-function (static factory method) which creates & returns instances,
hiding the details of class modules from user.
●
A factory pattern is one of the core design principles to create an object, allowing
clients to create objects of a library(explained below) in a way such that it doesn’t
have tight coupling with the class hierarchy of the library.
WHAT IS MEANT WHEN WE TALK ABOUT LIBRARY AND CLIENTS?
● A library is something which is provided by some third party which exposes some
public APIs and clients make calls to those public APIs to complete its task. A very
simple example can be different kinds of Views provided by Android OS.
13. 13
public class User {
private static volatile User instance = null;
// other fields / standard constructors / getters
public static User getSingletonInstance(String name, String email, String
country) {
if (instance == null) {
synchronized (User.class) {
if (instance == null) {
instance = new User(name, email, country);
}
}
}
return instance;
}
}
User user = User.getSingletonInstance("John", "john@domain.com", "Argentina");