Difference Between Constructor and Static Factory Method in Java - GeeksforGeeks

09 Jun, 2021
Whenever we are creating an object some piece of code will be executed to perform initialization of that object. This piece of code is nothing but a constructor, hence the main purpose of the constructor is to perform initialization of an object but not to create an object. Let us go through the basic set of rules for writing a constructor. They are as follows:
The name of the class and the name of the constructor must be the same.Return type concept is not applicable for constructor even void also. By mistake, if we are trying to declare return type for the constructor then we won’t get a compile-time error because the compiler treats it as a method.The only applicable modifiers for the constructor are public, private, protected, and default. If we are trying to use any other modifier we will get a compile-time error saying modifier name_of_modifier Not allowed here.
The name of the class and the name of the constructor must be the same.
Return type concept is not applicable for constructor even void also. By mistake, if we are trying to declare return type for the constructor then we won’t get a compile-time error because the compiler treats it as a method.
The only applicable modifiers for the constructor are public, private, protected, and default. If we are trying to use any other modifier we will get a compile-time error saying modifier name_of_modifier Not allowed here.
A compiler is responsible to generate the default constructor but not JVM. If we are not writing any constructor then only the compiler will generate the default constructor i.e if we are writing at least one constructor hence every class in java can contain a constructor it may be a default constructor generated by a compiler or customize constructor explicitly provided by the programmer but not both simultaneously.
The prototype of the default constructor is as follows:
It is always a no-arg constructor
The access modifier of the default constructor is exactly the same as the access modifier of the class.
It contains only one line super() it is a no-argument call to the superclass constructor.
By using a class name if we are calling a method and if that method returns the same class object then such type of method is called static factory method. The static factory methods are methods that return an instance of the native class. The static factory method has names that clarify the code, unlike the constructors. In the static factory method, we do not need to create a new object upon each invocation i.e object can be cached and reused if required. We can also return the subtype of their return type. 
The first line inside every constructor should be either super() or this() and if we are not writing anything then the compiler will always place super()
Example: 
Java
// Java Program to showcase Difference Between// Constructor and Static Factory method // Importing all utility classes from// java.util package// Importing all input output classesimport java.io.*;import java.util.*; // Main class// To find out complex numberpublic final class GFG {     // Method 1    // Static factory method returns an object of this    // class.    public static GFG valueOf(float real, float imaginary)    {        return new GFG(real, imaginary);    }     // Caller cannot see this private constructor.The only    // way to build a GFG is by calling the static factory    // method.     // Constructor    private GFG(float real, float imaginary)    {         // This keyword refers to current object itself        this.real = real;        this.imaginary = imaginary;    }     private float real;    private float imaginary;     // Method 2    // Main driver method    public static void main(String[] args)    {        // Creating an object of GFG and        // calling an static factory method valueOf()        GFG n = GFG.valueOf(2, 4);         // Print and display the complex number        System.out.println(n.real + "+" + n.imaginary                           + "i");    }}
Output:
Hence, from the above article, we can conclude the differences between them clearly which are as follows:
saurabh1990aror
Java-Constructors
Picked
Difference Between
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Difference between Internal and External fragmentation
Difference between Prim's and Kruskal's algorithm for MST
Differences and Applications of List, Tuple, Set and Dictionary in Python
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Arrays.sort() in Java with examples
Reverse a string in Java