I have created 2 timers in 2 separate classes. One timer increments int counter. and the other uses a get method and prints out the value of int counter.
The problem is the second timer only prints out 0, 0, 0 etc if i use private int counter
whereas if i were to use private static counter it prints out 1,2,3,4,5 etc which is what i want. But i would rather not use static because ive been told its bad practice.
Here is my main class:
import java.util.Timer;
public class Gettest {
public static void main(String[] args) {
classB b = new classB();
classC c = new classC();
timer = new Timer();
timer.schedule(b, 0, 2000);
Timer timer2 = new Timer();
timer2.schedule(c, 0, 2000); }}
class B with timer1
import java.util.TimerTask;
public class classB extends TimerTask {
private int counter = 0;
public int getint()
{ return counter;}
public void setint(int Counter)
{ this.counter = Counter;}
public void run()
{ counter++;
this.setint(counter);}}
class C with timer 2
import java.util.TimerTask;
public class classC extends TimerTask
{
classB b = new classB();
public void run(){
System.out.println(b.getint());}}
How could i fix so i it works using private int counter;?
bvariable to use intimer.schedule(b, 0, 2000)