0

I know that you cannot make reference to a static variable from a non-static method. Created an instance method to be called in my main method, and as you can see I made the main class non-static (atleast I think I did). The error that I am getting is on line 19 "Non-static method 'readLine()' cannot be referenced from a static context". Seems like such a simple problem but I can't wrap my head around it.


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PeerMain {

    public void main(String[] args) throws IOException
    {
        PeerMain PEER = new PeerMain();
        PEER.run();

    }

    public void run() {
        BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in)); //prompt user for input
        System.out.println("> Enter ID & port: ");
        String[] userInput =  BufferedReader.readLine().split(" "); // pick up user input
    }
}
justcain
  • 13
  • 3
  • replace `BufferedReader` with `bfr` on the line where you have error. also main method should be `static` and just fyi "I know that you cannot make reference to a static variable from a non-static method" is incorrect – Oleg Feb 26 '20 at 02:07
  • @oleg Thanks, that helped and adding an exception to the method signature fixed it completely. Why is it incorrect? – justcain Feb 26 '20 at 02:20
  • It is incorrect because you have it back to front. You can't make a reference to a *non*-static variable from a *static* method. – user207421 Feb 26 '20 at 04:13

1 Answers1

1

I know that you cannot make reference to a static variable from a non-static method.

No, you cannot reference a non-static variable from a static context, as it doesn't have a reference to any instance to do so. The other way around - that you try to document here - is perfectly possible.

Created an instance method to be called in my main method, and as you can see I made the main class non-static (atleast I think I did).

No, you made the main method non-static. Top level classes are always static even though the indicator is missing (you can "see" the constructor and the class definition from any static method).

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

// always static, because it is a top level class
public class PeerMain {

    // needs to be static to be run from command line or IDE
    public static void main(String[] args) throws IOException
    {
        // identifiers should be (lower) camelCase, starting with a lowercase "word"
        PeerMain peer = new PeerMain();
        // don't name your method `run` unless you're implementing `Runnable`
        peer.run();
    }


    // you forgot to throw IOException
    public void run() throws IOException {
        // use try-with-resources
        try (BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in))) {
            System.out.println("> Enter ID & port: ");
            // here you referenced the class definition, rather than the variable referencing the instance of BufferedReader
            String[] userInput =  bfr.readLine().split(" "); 
        }
    }
}

Prefer comments on the lines before the code rather than behind it, because end of line comments tend to move out of sight.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263