-1

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?

junxi
  • 174
  • 1
  • 13
  • 3
    Variables `arr` and `c` on lines 3 and 4 respectively need to be `static`. The method `counter(int, int[])` has nothing to do with it. – Gooz Jul 25 '17 at 09:20
  • 1
    the array `arr` your referencing is an instance variable, but `public static void main()` is in a static context (as `static`in the signature already states) – Lino Jul 25 '17 at 09:20
  • 1
    Otherwise you need to move them into the main method. – ChristofferPass Jul 25 '17 at 09:21
  • 1
    maybe read this guide on **static vs non static** http://beginnersbook.com/2013/05/static-vs-non-static-methods/ – Lino Jul 25 '17 at 09:22
  • Static method use only static members of the class. You can't use non static members of class inside static method. You have to make them static or create Test class object to use them. – Vishal Shevale Jul 25 '17 at 09:31

3 Answers3

1

You are referring the two variables from the inside of a static function, they need to be static themselves to avoid errors.

A static method is one that can be invoked without an object of the class. That means that there is no this reference and no instance variable allocated to access. Static variables, instead, are allocated when the class is loaded and are available to both static and non-static method.

In yout code, the problem is that you try to access the variable from inside the main method, which is always static.

bracco23
  • 2,181
  • 10
  • 28
1

The main method is in a static context, isn't it? Your array and counter declared in Test are not static.

You can never directly access non-static members in a static context.

That is why the IDE is suggesting that you make the fields static.

Changing the counter method to static doesn't really solve the problem because you are still trying to directly access arr in a static context.

Another way to fix this is to make both c and arr local variables:

public static void main(String[] args) {
    int []arr = {4, 21, 4};
    Counter c = new Counter();
    System.out.println(c.counter(4, arr));
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

A program's main method must not be tied to an instance of the class, so it's defined as static.

arr and c are declared as instance variables of Test, so they are members of each instance of that class. No such instance exists within the scope of the main method.

Any identifiers used in a static method must be one of: a) static members of the same class b) local variables, declared within the method c) in the global namespace (e.g. another class's static member, like Math.pi)

The easiest solution is to simply move the definitions of arr and c inside the main method. There's no reason for them to be members of the class given your usage.

public class Test{

    public static void main(String[] args) {
        int []arr = {4, 21, 4};
        Counter c = new Counter();
        System.out.println(c.counter(4, arr));
    }
}

If you need them to be members of the class, you can simply change your original code to make them static (i.e. static int []arr = {4, 21, 4};)

A more flexible solution might involve using command line arguments to specify the values, or if you want to use instance variables, instantiating the class inside the main method:

public class Test{

    int []arr = {4, 21, 4};
    Counter c = new Counter();

    public static void main(String[] args) {
        Test t = new Test();
        System.out.println(t.c.counter(4, t.arr));
    }
}

But ^ this is kind of gross.

cosmicFluke
  • 355
  • 1
  • 10