2

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;?

6
  • 1
    1. Please use standard Java formatting. Random on the fly formatting makes your code difficult for Java programmers to red. 2. Please create and post a valid minimal reproducible example so we can run, test, and modify real runnable code. You state "here's my main class" but that's not your entire main class. Instead create and post your small compilable and runnable program that demonstrates your problem for us, and not in a link but rather here with your question. Commented Nov 29, 2015 at 3:14
  • 1
    "i would rather not use static because i've been told its bad practice", everything is bad practice if you don't use them correctly. Commented Nov 29, 2015 at 3:17
  • @Hovercraft Full Of Eels i have edited the post to include everything in the class so it is runnable now. All which was needed was the imports. Any advice you could give me? Commented Nov 29, 2015 at 3:22
  • Sorry, but there is no way that I can compile nor run this code. Commented Nov 29, 2015 at 3:25
  • there's no b variable to use in timer.schedule(b, 0, 2000) Commented Nov 29, 2015 at 3:28

2 Answers 2

2

You have basically created two separate instance or so, called two different object in memory. So, how can instance of one object print value of another object. Either use, static counter or pass the reference to same object.

Sign up to request clarification or add additional context in comments.

Comments

1

You've got two completely unique/separate ClassB instances, one you run with a Timer, the other you display. The displayed one never changes since it's not run in a Timer, and so it will always display the initial default value of 0.

If you change it so you have only one instance:

import java.util.Timer;
import java.util.TimerTask;

public class Gettest {
    private static Timer timer;

    public static void main(String[] args) {
        ClassB b = new ClassB();
        ClassC c = new ClassC(b); // pass the B instance "b" into C
        timer = new Timer();
        timer.schedule(b, 0, 2000);
        Timer timer2 = new Timer();
        timer2.schedule(c, 0, 2000);
    }
}

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 ClassC extends TimerTask {
    ClassB b;

    // add a constructor to allow passage of B into our class
    public ClassC(ClassB b) {
        this.b = b;  // set our field
    }

    public void run() {
        System.out.println(b.getint());
    }
}

the code will work.

As a side recommendation, again, please work on your code formatting, and strive so that it conforms to Java standards. For example, please see my code above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.