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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:53:06+00:00 2026-05-24T04:53:06+00:00

Basically, I have an int value, x, that is an instance variable of a

  • 0

Basically, I have an int value, x, that is an instance variable of a class. It is set to 0 initially. I have another method, “getNum,” that increments and returns x. This getNum() method is called by an outside program. However, each day (let’s say, at midnight, for simplicity’s sake) I want to reset x to 0. How would this be done? The problems I’m having now: Date(int, int, int) is deprecated but the method only takes Dates, not Calendars; the TimerTask event never happens; every time I run the program at the bottom, it simply prints “0” every time even though the number should not be constantly resetting. Basically, nothing works. Any ideas what is going wrong?

import java.util.*;
class Foo {
  private static void scheduleTimer() {
  Timer timer = new Timer();
  long c = 86400000;
  timer.scheduleAtFixedRate(new MyTimerTask(),
  new Date(2011, 7, 31), c);
  }
  public static int getNum() { return x++; }
  private static int x = 0;
  public static void resetNum() { x = 0; }
}

class MyTimerTask() extends TimerTask {
  public void run() {  
    Foo.resetNum(); 
  }

public class Bar { // in a separate file
public static void main (String[] args) {
   System.out.println(Foo.getNum());
  }
}
  • 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-24T04:53:07+00:00Added an answer on May 24, 2026 at 4:53 am

    Fix your two compilation errors, call Foo.scheduleTimer() somewhere to actually start the timer, give the timer a reasonable delay and period (like a few seconds), and then call Foo.getNum() repeatedly instead of just once. For example:

    import java.util.Timer;
    import java.util.TimerTask;
    
    public class Foo {
        public static void scheduleTimer() {
            Timer timer = new Timer();
            long c = 3000;
            timer.scheduleAtFixedRate(new MyTimerTask(), c, c);
        }
    
        public static int getNum() { return x++; }
        private static int x = 0;
        public static void resetNum() { x = 0; }
    }
    
    class MyTimerTask extends TimerTask {
        public void run() {
            Foo.resetNum();
        }
    }
    
    class Bar {
        public static void main(String[] args) throws Exception {
            Foo.scheduleTimer();
            while (true) {
                System.out.println(Foo.getNum());
                Thread.sleep(1000);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called Atomic which is basically an _Atomic_word plus methods that
Basically you have two ways for doing this: for (int x = 0; x
I basically have a server set up and I'm accepting new clients(UNIX) and i'm
I basically have 7 select statements that I need to have the results output
I basically have to do a update of a record. I have a set
I have a device driver that uses the following algorithm to convert a value
So basically I have this if statement: int md; // md = marriage deduction
Suppose I have a Singleton class (any class can get the instance): class data
I have a class that has this in the initializer: @implementation BaseFooClass -(id) init
We have a class library that does some basic operations similar to an ORM,

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.