Difference Between Constructor and Static Factory Method in Java Last Updated : 09 Jun, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 constructorThe 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 methodThe 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() Comment More infoAdvertise with us Next Article Difference Between Constructor and Static Factory Method in Java M mroshanmishra0072 Follow Improve Article Tags : Java Technical Scripter Difference Between Java-Constructors Practice Tags : Java Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Like