-2

I am writing this simple code but I have an error at last line:

"Cannot make a static reference to the non-static field br"

Kindly explain me why I have this error. I can suppress the error if I make the variable static, like static BufferedReader br or if I declare it inside the main method.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.Object;

public class Solution {
    BufferedReader br;

    public static void main(String[] args) {

        br = new BufferedReader(new InputStreamReader(System.in));

    }

}
Pawel Veselov
  • 3,996
  • 7
  • 44
  • 62
  • 1
    Does this answer your question? [Android - Cannot make a static reference to the non-static field](https://stackoverflow.com/questions/16982417/android-cannot-make-a-static-reference-to-the-non-static-field) – M. Dudek Jun 15 '21 at 18:13
  • Make `br` a local variable of `main` instead of an instance variable of the class. – GriffeyDog Jun 15 '21 at 18:14

1 Answers1

0

Try initializing your BufferedReader inside main() like so:

BufferedReader bfr = new BufferedReader(...);

Since you've initialized it outside the method it's treated as an attribute of the class and thus you either have to make it static or create an object of your class in order to access it.

JohnBoy
  • 3
  • 1
  • 5