MarkLoveExEx
asked on
Java Timer (static)
Can I make a Timer static, so that it is shared by all objects of a class? I do not want each instance of my class to have its own Timer.
I was able to make my TimerTask static, as follows:
However, within my class, I cannot make a static Timer:
Both the above static lines give me similar errors, like this:
Illegal modifier for parameter fetchCounts; only final is permitted
What am I doing wrong? How do I make one Timer for all objects of the class?
I was able to make my TimerTask static, as follows:
public static class UpdateCounts extends TimerTask {
@Override
public void run() {
//update some other static variable
}
}
However, within my class, I cannot make a static Timer:
static TimerTask fetchCounts = new UpdateCounts();
static Timer timerCounts = new Timer();
timerCounts.scheduleAtFixedRate(fetchCounts, 2000, 1000);
Both the above static lines give me similar errors, like this:
Illegal modifier for parameter fetchCounts; only final is permitted
What am I doing wrong? How do I make one Timer for all objects of the class?
Only the Timer need be static.
The compilation error suggests you're trying to use the static keyword inside a method or constructor. You can't do that. If you declare and initialize your static field in the body of the class itself, outside of any method or constructor, it should work better.
ASKER
I did not know you could not have a static keyword in a constructor. I created a class outside the constructor and am now attempting to use that. The original error is gone. However, I now have a runtime error:
Inside my class, but outside the constructor, I have:
Then, I have the class:
Then, my TimerTask, as follows:
Inside the constructor, I have:
My runtime error:
java.lang.IllegalStateExce ption: Task already scheduled or cancelled
Inside my class, but outside the constructor, I have:
static Class4StaticTimer callMyTimer = new Class4StaticTimer();
Then, I have the class:
public static class Class4StaticTimer {
TimerTask fetchCounts = new UpdateCounts();
static Timer timerCounts = new Timer();
}
Then, my TimerTask, as follows:
public static class UpdateCounts extends TimerTask {
@Override
public void run() {
System.out.println("UpdateCounts run"):
}
}
Inside the constructor, I have:
callMyTimer.timerCounts.scheduleAtFixedRate(callMyTimer.fetchCounts, 2000, 1000);
My runtime error:
java.lang.IllegalStateExce
Yeah, you can only schedule it once. If you need each instance of your class to schedule a timer, then the timer can't be static.
ASKER
I only want to schedule it once...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
All this static stuff smacks of a fundamental design problem: you probably need to look at that before it gets worse if that's the case
http://technojeeves.com/index.php/aliasjava1/108-static-is-a-swear-word
http://technojeeves.com/index.php/aliasjava1/108-static-is-a-swear-word
ASKER
Thank you! In all my Java programming, I have never used a static block. It is weird that it doesn't have a name. I guess it is just run when the class initializes. You have definitely taught me a few things about "going static". :)
ASKER
CEHJ, I do not do much with static anything. But in this case, I needed a Timer in the class, one that is shared by all the classes.