I want the variable called "number" to stay the same value when the method is called multiple times. It seems like it resets between each method call. I don't see why it would because the variable is declared outside the method.
This is the first class:
import java.util.Scanner;
public class Input {
public static void main(String[] args) {
for(int counter = 0; counter < 5; counter++) {
Output display = new Output();
display.outputNumber();
}
}
}
This is the second class:
public class Output {
int number;
public void outputNumber() {
number++;
System.out.println(number);
}
}
When I run this, it outputs
1
1
1
1
1
I want it to output:
1
2
3
4
5