1.
Factory Methods
Introduction
In Java, object creation is usually done using the new keyword.
However, in many cases, we may not want the programmer to directly create an object using
new.
Instead, we use factory methods, which are special static methods that return an instance
(object) of a class.
These methods act like a factory that “manufactures” and provides objects to the user — hence
the name factory methods.
Definition
A factory method is a static method that returns an instance of a class.
It is used when:
Object creation is complex or controlled.
We want to hide the internal details of how the object is created.
We want to reuse or share existing objects instead of always creating new ones.
Key Characteristics of Factory Methods
1. They are declared as static, so they can be called without creating an object.
2. They return an object of the class (or a subclass).
3. They hide the creation logic from the user.
4. They often have meaningful names like getInstance(), getObject(), or getByName() to
describe what kind of object they return.
Example 1 – InetAddress Class
In Java’s networking package ([Link]), the class InetAddress represents an IP address of a
host (either IPv4 or IPv6).
We cannot create its object directly using new.
Instead, we use factory methods provided by the class itself.
InetAddress address = [Link]("[Link]");
[Link]("Host Name: " + [Link]());
[Link]("IP Address: " + [Link]());
Here,
getByName() is a factory method.
It returns an InetAddress object representing the given host.
We didn’t write new InetAddress(), but still, we got an object.
Common Factory Methods in InetAddress
Method Description
getLocalHost() Returns the IP address of the current system.
getByName(String host) Returns the IP address of the given host name.
getAllByName(String host) Returns all possible IP addresses for the given host.
getByAddress(byte[] addr) Returns the object based on a byte array of an IP address.
Advantages of Factory Methods
Encapsulation of object creation (the logic is hidden).
Control over object creation — we can decide whether to create a new object or return an
existing one.
Improved readability — method names clearly describe the returned object.
Flexibility — different subclasses can be returned based on input parameters.
In Simple Words
A factory method is like a “ready-made” object supplier —
you don’t have to know how the object is made; you just ask for it and get it.
2. Instance Methods
Introduction
Instance methods are the most common type of methods in Java.
They belong to an instance (object) of a class and can access the instance variables of that
class.
Each time an object is created, it has its own copy of instance variables, and instance methods
can operate on that data.
Definition
An instance method is a non-static method that performs operations on instance variables or
other instance methods of the same class.
To call an instance method, we must first create an object of that class.
Example
class Student {
String name;
int age;
void displayInfo() { // Instance method
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
[Link] = "Nargis";
[Link] = 22;
[Link](); // Calling instance method
}
}
Explanation:
displayInfo() is an instance method.
It can access the instance variables name and age.
It is called through an object ([Link]()).
Features of Instance Methods
1. They are non-static, and hence need an object to call them.
2. They can access instance variables and instance methods directly.
3. They can also access static members if needed.
4. Each object has its own version of data on which the method operates.
Example – InetAddress Instance Methods
The InetAddress class provides instance methods that operate on the object created by its
factory methods.
InetAddress ip = [Link]("[Link]");
[Link]("Host Name: " + [Link]());
[Link]("Host Address: " + [Link]());
Here,
getHostName() and getHostAddress() are instance methods.
They operate on the ip object, which was created by the factory method getByName().
Advantages of Instance Methods
● They make code object-oriented and easy to reuse.
● They allow different objects to maintain different states.
● They help in implementing real-world behavior of objects (like actions).
Summary in One Line
> Factory methods create objects,
while instance methods use those objects to perform actions.