DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Writing DTOs With Java8, Lombok, and Java14+
  • Enabling Behavior-Driven Service Discovery: A Lightweight Approach to Augment Java Factory Design Pattern
  • Redefining Java Object Equality
  • Addressing Memory Issues and Optimizing Code for Efficiency: Glide Case

Trending

  • AI Speaks for the World... But Whose Humanity Does It Learn From?
  • Four Essential Tips for Building a Robust REST API in Java
  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  • Distributed Consensus: Paxos vs. Raft and Modern Implementations
  1. DZone
  2. Coding
  3. Languages
  4. Java 8/9 Best Practices — Part-1 (Static Factory Methods)

Java 8/9 Best Practices — Part-1 (Static Factory Methods)

By 
Nitesh Gupta user avatar
Nitesh Gupta
·
Updated Jul. 23, 20 · Opinion
Likes (5)
Comment
Save
Tweet
Share
8.0K Views

Join the DZone community and get the full member experience.

Join For Free

Should Consider Static Factory Methods for Creating Objects

There are various ways to create the objects in java, In a traditional way for the class to allow a client to create an instance is to provide the public default constructor. However, there is another way to create an instance of the Class. A class can provide a static public method which is a static method that returns the instance of the class. Let's see the example

Java
 




x


 
1
public static Boolean valueOf(boolean a){
2
  return a ? Boolean.TRUE : Boolean.FALSE;
3
}



There are various advantages and disadvantages to consider Static Methods -

Advantages

  1. Static Methods have a name
  2. Not required to create an object every time it gets invoked
  3. They can return an object of any subtype of their return type
  4. Class of the returned object can vary from call to call as a function of the input parameters
  5. Class of the returned object need not exist when the class containing the method is written

Limitations

  1. A class without public or protected constructor can not be sub-classed if we only expose static Method for object creation
  2. It could be difficult for the programmer to find the method where the object is being created unlike a constructor

Some common names for static factory methods

Java
 




xxxxxxxxxx
1


 
1
Date d = Date.from (instance); //from
2
Set<Status> statuses = EnumSet.of(INITIATED, INPROGRESS,COMPLETED);//Use of Of
3
String str1 = String.valueOf(Integer.MAX_VALUE); // use of valueOf
4
Object newArray = Array.newInstance(classObject, length) // use of newInstance
5
BufferedReader br = Files.newBufferedReader(path);//use of newType
6
 
          



Both Static factory methods and public constructors both have their users. Static factories are preferable, so avoid the defining public constructor and think if Static factories can be used.

Ref - Effective Java - Third Edition by Joshua Bloch

Factory (object-oriented programming) Java (programming language) Object (computer science)

Opinions expressed by DZone contributors are their own.

Related

  • Writing DTOs With Java8, Lombok, and Java14+
  • Enabling Behavior-Driven Service Discovery: A Lightweight Approach to Augment Java Factory Design Pattern
  • Redefining Java Object Equality
  • Addressing Memory Issues and Optimizing Code for Efficiency: Glide Case

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!