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

  • SEARCH
  • Home
  • 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 8713383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:19:54+00:00 2026-06-13T05:19:54+00:00

This is more of a style question than an I’m having trouble question. For

  • 0

This is more of a style question than an “I’m having trouble question”. For Null placeholder objects (am I using the right term?), is it generally preferred to use the Singleton pattern? For ease of discussion, here’s an example:

public interface Foo {
    void myMethod();
}

public class RealFoo implements Foo {
    void myMethod() { /* Do something productive */ }
}

public class MyUniverse {
    public static void main(String args[]) {
        Foo[] fooArray = new Foo[10];
        // do something productive that might result in Null objects in the array
        for (Foo f : fooArray) {
            f.myMethod(); // I DONT WANT TO DO if (f != null) blah blah
        }
    }
}

Ok so that’s my scenario, is it preferred to do A or B?

// A
public class NullFoo implements Foo {
    public NullFoo() {}       
    public void myMethod() { /* don't need to do anything */ }
}

// B
public class NullFoo implements Foo {
    private static NullFoo _instance = null;
    protected NullFoo() {}
    public static NullFoo getInstance() {
        if (_instance == null) _instance = new NullFoo();
        return _instance;
    }
    public void myMethod() {  /* don't need to do anything */ }
}

Thanks! My instinct that B is almost always superior but maybe I’m missing something, so I ask…

  • 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-06-13T05:19:56+00:00Added an answer on June 13, 2026 at 5:19 am

    I would go for the latter. I may be something of a trend-bucker on this, but I don’t see singletons as inherently evil. If a class has no mutable state, a singleton is just fine.

    I’d make the constructor private, though, not protected. Also, your lazy loading is race-prone; I would just instantiate NullFoo on the declaration line (private static final Foo instance = new NullFoo();). And lastly, don’t forget to have the class actually implement the interface. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think this question is more of a coding style rather than technical issue.
This is a question that involves style more than anything. I'm looking for ideas
This more of a style question, rather than a how to. So I've got
This is probably more a design or style question: I have just been considering
This isn't a style question. Its more about the proper use of the language
This question is probably more a matter of syntax than anything else. I have
This is more than likely a very simple question. I have an external javascript
This is a question more out of curiosity rather than being stuck. I know
This is more of a general best practice question rather than a very focused
This is a question about style and design rather than syntax. I have domain

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.