Simple code, counting the number n in a given array, but I get an error which tells me to change my instancevariable to static.
public class Test{
int []arr = {4, 21, 4};
Counter c = new Counter();
public static void main(String[] args) {
System.out.println(c.counter(4, arr));
}
}
public class Counter {
public int counter(int n, int []ar){ //tried to change method to static - didn't help
int counter = 0;
for(int i = 0; i < ar.length; i++){
if(n == ar[i]){
counter++;
}
}
return counter;
}
}
The error appears in line 7: change int [] arr
and Counter c
to static.
Tried to make the counter method static but still it tells me to change my array to static. What's the reason?