Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 906851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:27:19+00:00 2026-05-15T16:27:19+00:00

When and how should we use a constructor Foo bar = new Foo(); And

  • 0
  1. When and how should we use a constructor

    Foo bar = new Foo();
    
  2. And when and how should we use getInstance() (static factory methods)

    Foo bar = Foo.getInstance();
    

What is the difference between these two? I have always used a constructor, but when should I use getInstance() instead?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:27:20+00:00Added an answer on May 15, 2026 at 4:27 pm

    Everybody seems to focus on singletons while I think that the question is actually about constructor vs static factory methods.

    This is actually Item 1: Consider static factory methods instead of constructors of Effective Java by Joshua Bloch:

    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. 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.

    Advantages (quoting the book):

    • One advantage of static factory methods is that, unlike constructors, they have names.
    • 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.
    • A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type.
    • A fourth advantage of static factory methods is that they reduce the verbosity of creating parameterized type instances.

    Disadvantages (still quoting the book):

    • The main disadvantage of providing only static factory methods is that
      classes without public or protected constructors cannot be subclassed.
    • A second disadvantage of static factory methods is that they are not
      readily distinguishable from other static methods.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

should i use Classname() or __construct() as constructor in CodeIgniter? both work, which should
I don't know which title I should use for this question. I have a
First let's establish this. I have public abstract class Foo { public static void
I have a class Foo<T, U> with the following constructor: public Foo() { clazz
I have to use functionality that is in another application domain. The result should
We have php 5.3.0 and I would like to use the constructor _construct. But
I've read I should use money , but in todays fast paced world maybe
Why sometimes I should use block and other times &block inside functions that accept
I know you should use POST whenever data will be modified on a public
I wonder how I should use the GTree (from GLib) to store data? Every

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.