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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:47:46+00:00 2026-06-07T09:47:46+00:00

Basically, I want to create Counter objects, all they have to do is hold

  • 0

Basically, I want to create Counter objects, all they have to do is hold number values. And in my resetCounters method, I would like to reset each object’s values. This is probably very easy, but I’m a newb.

public class Counter
{
    Random number = new Random();

    Counter()
    {
        Random number = new Random();
    }

    public Random getNumber()
    {
        return number;
    }

    public void setNumber(Random number)
    {
        this.number = number;
    }

    public static void main(String[] args) 
    {
        Counter counter1 = new Counter();
        Counter counter2 = new Counter();
        Counter counter3 = new Counter();
        Counter counter4 = new Counter();
        Counter counter5 = new Counter();

    }

    public static void resetCounters()
    {

    }
    }
  • 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-07T09:47:47+00:00Added an answer on June 7, 2026 at 9:47 am

    First option: Memorize each instance of Counter.

    Collect each instance of Counter in some static collection.
    To reset all, simply iterate over all items in the collection.
    But strong references are too strong for this — make sure it’s a collection of weak references.

    Remarks:

    1. Using weak references will avoid the issue that the Counter objects exist indefinitely only because of their reference from within the static collection. Objects that are referred to only by weak references are eventually collected by the garbage collector.
    2. The collection of every instance can be achieved by declaring the Counter constructor private and allowing only construction through a static member function which will also do the registration. (Or use some other incarnation of the Factory pattern.) I believe a factory is the way to go here, since each construction of an object has to carry out also a side effect. But perhaps it will make do to have the Counter constructor register this with the static collection.

    Second option: Generation counter

    Keep a static generation counter of type long, and also a copy of this counter in each instance. When resetting all counters, just increase the static generation counter. The getNumber() method will then check the static generation counter against its own copy and reset the counter if the static generation counter has changed.

    (I don’t really know the “official” name for this trick. How to zero out array in O(1)?)

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

Sidebar

Related Questions

Basically I want to create a method that has a signature like the following:
I have php generating html and I want to create layers. Basically I want
I want to create own filetype to save objects in my app. Basically, I
I have a method CreateAccount(...) that I want to unit test. Basically it creates
Basically I want to create 48 button outlets. I have created the buttons in
Basically I want to create a php code in my template (views-view-field--body.tpl.php) that would
I basically want to create something like this: So, a user can add/remove items
Basically I want to create one large object of many object in JavaScript. Something
So basically I want to create a GUI app using PySide and the Qt
I want to create a Google Docs document from within Haskell, so basically I

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.