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 9250973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:34:52+00:00 2026-06-18T10:34:52+00:00

I have 0..n objects, each of which requires a unique identifier, that are created

  • 0

I have 0..n objects, each of which requires a unique identifier, that are created in this way:

public class Squirrel {

    private static numSquirrels = 0;

    private String id = null;

    public Squirrel() {
        this(String.valueOf(numSquirrels++);
    }

    public Squirrel(String id) {
        this.id = id;
    }
}

This is a problem in a few ways but I’ll give one:

When unit testing, the numSquirrels variable carries over between
tests, even though I may be working with a different population of
squirrels. That means that their IDs continue to increment when I’d
like to start fresh.

  • Is this the correct time to use a SquirrelFactory (the ones the kids are raving about)?
  • Should I pass the Factory into the Squirrel object using dependency injection, or should the Squirrel class be contained inside the Squirrel Factory which has an interface to the outside world?
  • How do I ensure uniqueness if I want the user to be able to set the ID (or at least suggest IDs)?
  • 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-18T10:34:53+00:00Added an answer on June 18, 2026 at 10:34 am

    I think this implementation will work fine, although you should probably use AtomicInteger if you are dealing with concurrency.

    Your unit testing problem can be solved using a combination of mocking and wrapping it in another class to mock. See this post
    How to mock a static variable in java using JMock

    Alternatively, a simple solution would be to expose a setter for the static variable, in which at the end of your test cases, you can set it back to 0, or whatever “reset” means to you.

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

Sidebar

Related Questions

I have parsed XML file into objects, in which each object has a 1:1
I have 3 Lists which contains objects of type Book. Each book has a
I have several dozen objects exposed through COM interfaces, each of which with many
I have a worker class that does stuff with a collection of objects. I
I have a system that requires a unique 6-digit code to represent an object,
I have a Rotation class, which contains references to multiple lists of Advert objects.
This is a Hibernate/JPA question. I have a set of Schedule objects, each including
I have created objects that are interfaces to a web service. One typical object
Say I have a list of member, each of which is a custom object:
I have an 'superior object' of type HierarchyNode which will have childnodes (each child

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.