Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Static factory method

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Static factory method

    What is 'Static factory method'?. I have seen a topic about static factory method from "Effective java" by Joshua Bloch. Here the writer explained it's advantages compared to constructor.But there is no complete example. pls help me anyone with appropriate example . Thanks to all...


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Static factory method

    Please copy the full description of 'Static factory method' from the book so we can see what the author is talking about. Did the author give any examples from the Java SE classes?

    What does google say?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Static factory method

    A factory is a whole class designed to create objects for you. They sometimes have multiple ways of getting the object so even though you are getting the same object either way, it is configured in a special way. Static just refers to the fact that you never have an instance of the factory, you just call the method from the class. Think of it like a real life car factory, i could create my car myself and make it exactly how i want or i could call up the factory and have them make me the one i want. So to put that in programming terms you would call the factory with your parameters and they would hand you back your car object configured exactly how you want. This is much more handy when you have very abstract objects that can be configured in many different ways. With basic objects it is typically overkill.

  4. #4
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Static factory method

    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. Here’s a simple example
    from Boolean (the boxed primitive class for the primitive type boolean). This
    method translates a boolean primitive value into a Boolean object reference:
    public static Boolean valueOf(boolean b) {
    return b ? Boolean.TRUE : Boolean.FALSE;
    }
    Note that a static factory method is not the same as the Factory Method pattern
    from Design Patterns [Gamma95, p. 107]. The static factory method described in
    this item has no direct equivalent in Design Patterns.
    A class can provide its clients with static factory methods instead of, or in
    addition to, constructors. Providing a static factory method instead of a public
    constructor has both advantages and disadvantages.

    One advantage of static factory methods is that, unlike constructors, they
    have names.
    If the parameters to a constructor do not, in and of themselves,
    describe the object being returned, a static factory with a well-chosen name is easier
    to use and the resulting client code easier to read. For example, the constructor
    BigInteger(int, int, Random), which returns a BigInteger that is probably
    prime, would have been better expressed as a static factory method named BigInteger.
    probablePrime. (This method was eventually added in the 1.4 release.)
    A class can have only a single constructor with a given signature. Programmers
    have been known to get around this restriction by providing two constructors
    whose parameter lists differ only in the order of their parameter types. This is a
    really bad idea. The user of such an API will never be able to remember which
    constructor is which and will end up calling the wrong one by mistake. People
    reading code that uses these constructors will not know what the code does without
    referring to the class documentation.
    Because they have names, static factory methods don’t share the restriction
    discussed in the previous paragraph. In cases where a class seems to require multiple
    constructors with the same signature, replace the constructors with static factory
    methods and carefully chosen names to highlight their differences

    A second advantage of static factory methods is that, unlike constructors,
    they are not required to create a new object each time they’re invoked
    . This
    allows immutable classes (Item 15) to use preconstructed instances, or to cache
    instances as they’re constructed, and dispense them repeatedly to avoid creating
    unnecessary duplicate objects. The Boolean.valueOf(boolean) method illustrates
    this technique: it never creates an object. This technique is similar to the
    Flyweight pattern [Gamma95, p. 195]. It can greatly improve performance if
    equivalent objects are requested often, especially if they are expensive to create.
    The ability of static factory methods to return the same object from repeated
    invocations allows classes to maintain strict control over what instances exist at
    any time. Classes that do this are said to be instance-controlled. There are several
    reasons to write instance-controlled classes. Instance control allows a class to
    guarantee that it is a singleton (Item 3) or noninstantiable (Item 4). Also, it allows
    an immutable class (Item 15) to make the guarantee that no two equal instances
    exist: a.equals(b) if and only if a==b. If a class makes this guarantee, then its clients
    can use the == operator instead of the equals(Object) method, which may
    result in improved performance. Enum types (Item 30) provide this guarantee.


    A third advantage of static factory methods is that, unlike constructors,
    they can return an object of any subtype of their return type.
    This gives you
    great flexibility in choosing the class of the returned object.
    One application of this flexibility is that an API can return objects without
    making their classes public. Hiding implementation classes in this fashion leads to
    a very compact API. This technique lends itself to interface-based frameworks
    (Item 18), where interfaces provide natural return types for static factory methods

    Interfaces can’t have static methods, so by convention, static factory methods for
    an interface named Type are put in a noninstantiable class (Item 4) named Types.
    For example, the Java Collections Framework has thirty-two convenience
    implementations of its collection interfaces, providing unmodifiable collections,
    synchronized collections, and the like. Nearly all of these implementations are
    exported via static factory methods in one noninstantiable class (java.util.Collections).
    The classes of the returned objects are all nonpublic.

    --- Update ---

    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. Here’s a simple example
    from Boolean (the boxed primitive class for the primitive type boolean). This
    method translates a boolean primitive value into a Boolean object reference:
    public static Boolean valueOf(boolean b) {
    return b ? Boolean.TRUE : Boolean.FALSE;
    }
    Note that a static factory method is not the same as the Factory Method pattern
    from Design Patterns [Gamma95, p. 107]. The static factory method described in
    this item has no direct equivalent in Design Patterns.
    A class can provide its clients with static factory methods instead of, or in
    addition to, constructors. Providing a static factory method instead of a public
    constructor has both advantages and disadvantages.

    One advantage of static factory methods is that, unlike constructors, they
    have names.
    If the parameters to a constructor do not, in and of themselves,
    describe the object being returned, a static factory with a well-chosen name is easier
    to use and the resulting client code easier to read. For example, the constructor
    BigInteger(int, int, Random), which returns a BigInteger that is probably
    prime, would have been better expressed as a static factory method named BigInteger.
    probablePrime. (This method was eventually added in the 1.4 release.)
    A class can have only a single constructor with a given signature. Programmers
    have been known to get around this restriction by providing two constructors
    whose parameter lists differ only in the order of their parameter types. This is a
    really bad idea. The user of such an API will never be able to remember which
    constructor is which and will end up calling the wrong one by mistake. People
    reading code that uses these constructors will not know what the code does without
    referring to the class documentation.
    Because they have names, static factory methods don’t share the restriction
    discussed in the previous paragraph. In cases where a class seems to require multiple
    constructors with the same signature, replace the constructors with static factory
    methods and carefully chosen names to highlight their differences

    A second advantage of static factory methods is that, unlike constructors,
    they are not required to create a new object each time they’re invoked
    . This
    allows immutable classes (Item 15) to use preconstructed instances, or to cache
    instances as they’re constructed, and dispense them repeatedly to avoid creating
    unnecessary duplicate objects. The Boolean.valueOf(boolean) method illustrates
    this technique: it never creates an object. This technique is similar to the
    Flyweight pattern [Gamma95, p. 195]. It can greatly improve performance if
    equivalent objects are requested often, especially if they are expensive to create.
    The ability of static factory methods to return the same object from repeated
    invocations allows classes to maintain strict control over what instances exist at
    any time. Classes that do this are said to be instance-controlled. There are several
    reasons to write instance-controlled classes. Instance control allows a class to
    guarantee that it is a singleton (Item 3) or noninstantiable (Item 4). Also, it allows
    an immutable class (Item 15) to make the guarantee that no two equal instances
    exist: a.equals(b) if and only if a==b. If a class makes this guarantee, then its clients
    can use the == operator instead of the equals(Object) method, which may
    result in improved performance. Enum types (Item 30) provide this guarantee.


    A third advantage of static factory methods is that, unlike constructors,
    they can return an object of any subtype of their return type.
    This gives you
    great flexibility in choosing the class of the returned object.
    One application of this flexibility is that an API can return objects without
    making their classes public. Hiding implementation classes in this fashion leads to
    a very compact API. This technique lends itself to interface-based frameworks
    (Item 18), where interfaces provide natural return types for static factory methods

    Interfaces can’t have static methods, so by convention, static factory methods for
    an interface named Type are put in a noninstantiable class (Item 4) named Types.
    For example, the Java Collections Framework has thirty-two convenience
    implementations of its collection interfaces, providing unmodifiable collections,
    synchronized collections, and the like. Nearly all of these implementations are
    exported via static factory methods in one noninstantiable class (java.util.Collections).
    The classes of the returned objects are all nonpublic.

  5. #5
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Static factory method

    I think you may be getting hung up on the terminology. "Factory" just refers to a design pattern. It's just an idea. "Public" is the visibility and "Static" refers to the fact that it can be called from the class instead of it being part of a specific object(instance of class). So in their example, Boolean has a factory method that takes a primitive boolean and gives you back an instance of the Boolean class configured with the value of the primitive.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  6. #6
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Static factory method

    But under the short Booealn class example , writer said that ...

    "Note that a static factory method is not the same as the Factory Method pattern
    from Design Patterns [Gamma95, p. 107]. The static factory method described in
    this item has no direct equivalent in Design Patterns."

  7. #7
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Static factory method

    Yeah and he's right in a sense. The factory design pattern has to do with having a whole new class that is a factory used to pump out your objects. What he is referring to as a factory method is just one method within a class that builds and gives you the instance instead of doing a "new Object()" call. Still similar IMO but yeah a little different.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  8. The Following User Says Thank You to Chris.Brown.SPE For This Useful Post:

    Mahfuz Ahmed (March 21st, 2013)

Similar Threads

  1. Static and non-static method
    By irpersian20 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 18th, 2012, 11:57 AM
  2. Replies: 1
    Last Post: April 3rd, 2012, 06:32 AM
  3. What's the difference between a static and non-static method?
    By wholegrain in forum Java Theory & Questions
    Replies: 4
    Last Post: February 23rd, 2012, 01:06 AM
  4. non-static method cannot be referenced from a static context
    By Kaltonse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2010, 07:51 PM
  5. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM