• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What are static factory methods?

 
Ranch Hand
Posts: 40
2
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

While studying K&B book for OCP, chapter 08 states that we can use static factory methods to create an instance of Calendar class. My question is why its been called the "Static factory method" instead of static method. Where the difference lies?

Thanks.
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Obviously, we agree they are "static methods". The issue seems to be with the usage of the word "factory". In Java syntax, there is no such thing as a factory method (its not like you can mark a method with a 'factory' modifier), but factory is a "pattern" not syntax.

In other words, it means these methods are "conceptually using the factory pattern". You can read more about the factory pattern here: https://en.wikipedia.org/wiki/Factory_method_pattern
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static factory method is a method that creates an object and returns a reference to that object.
 
Kaleem Anwar
Ranch Hand
Posts: 40
2
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:Static factory method is a method that creates an object and returns a reference to that object.



so, any static method that returns a reference to an object will be called a static factory method and otherwise it is called a simple static method if not returning a reference, right??
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, a static method that returns a reference to an object is not always a static factory method. As stated earlier, the class has to implement the factory pattern, which is a conceptual pattern, not an interface.
 
Kaleem Anwar
Ranch Hand
Posts: 40
2
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scott Selikoff wrote:No, a static method that returns a reference to an object is not always a static factory method. As stated earlier, the class has to implement the factory pattern, which is a conceptual pattern, not an interface.



sorry i am not getting it

Does the calendar class in java implemented that factory pattern?? What actual features the method have once we call it a static factory method and what it deprived off if we call it a simple static method in Java. Also, how it can be translated to the actual code in Java.
 
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kaleem Anwar wrote:My question is why its been called the "Static factory method" instead of static method. Where the difference lies?


It's called a static factory method because it returns an instance of the class.

According to Joshua Bloch (Effective Java™, Second Edition)

Item 1: Consider static factory methods instead of constructors
The normal way for a class to allow a client to obtain an instance of itself is to provide a public constructor. There is another technique that should be a part of every programmer’s toolkit. A class can provide a public static factory method, which is simply a static method that returns an instance of the class.



Note that a static factory method is not the same as the Factory Method pattern from Design Patterns [Gamma95, p. 107].

 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll correct myself.

A factory method doesn't need to create a new instance every time it's called.

This is actually an advantage over constructors as it allows reusing objects.
 
reply
    Bookmark Topic Watch Topic
  • New Topic