How to resolve 'error: non-static variable cannot be referenced from a static context' in java?

Example:

public class Test {
    int num = 10;

    public static void main(String[] args) {
        System.out.println(num);
    }
}

The error means you're trying to access a non-static variable from a static method or context.

Solution:

Make the variable static or create an instance of the class to access it.


public static int num = 10;

public static void main(String[] args) {
    System.out.println(num);
}

Beginner's Guide to Java