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.
Default Constructor
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.
Static Factory Methods
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 classes import java.io.*; import java.util.*; // Main class // To find out complex number public 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:
Constructor | Static factory method |
---|---|
The constructor doesn’t have a meaningful name, so they always restricted to the standard naming convention | The static factory method can have a meaningful name hence we can explicitly convey what this method does. |
Constructors can’t have any return type not even void. | Static factory methods can return the same type that implements the method, a subtype, and also primitives. |
Inside the constructor, we can only perform the initialization of objects. | Inside static factory method other than initialization if we want to perform any activity for every object creation like increasing count value for every object creation we can do this in the static factory method. |
Constructor always creates a new object inside the heap, so it is not possible to return a cached instance of the class from a constructor. | But Factory methods can take advantage of caching i.e we can return the same instance of Immutable class from the factory method instead of always creating a new object. |
The first line inside every constructor should be either super() or this() | But inside the factory method, it is not necessary that the first line must be either super() or this() |