Free: 4 trials of 20 minutes, no cardStart free

Craftstack Academy
Java

Java: non-static method cannot be referenced from a static context

· 9 min read

You write a small helper method, call it from main, and javac refuses:

Terminal
Main.java:7: error: non-static method add(int,int) cannot be referenced from a static context
        System.out.println(add(2, 3));
                           ^
1 error

non-static method cannot be referenced from a static context means you called a method that belongs to an object from a place that has no object: the static method main. Java doesn't know which object's method you mean, so it stops. The fix is one of two things: mark the method static, or create an object and call the method on it. This article explains what static means in plain words, so you can choose the right fix every time.

Try it yourself

Save this as Main.java and run javac Main.java:

Main.java
public class Main {
    int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        System.out.println(add(2, 3));
    }
}

You'll get the error above. The method add looks perfectly normal. The only thing missing is one word.

What "static" means

A class is a blueprint. From one blueprint, you can make many objects. Think of a class Account: every account object has its own balance.

  • Anything without static belongs to each object. Every account has its own balance and its own deposit method that changes that balance. To use it, you need a specific object: which account?
  • Anything with static belongs to the class itself, shared by all objects. It can be used without creating any object.

main is always static, because Java has to start your program before any of your objects exist. So inside main, there's no "current object". When you call a non-static method there, Java asks: on which object? There isn't one, and that's the error.

Read the message in parts:

  • non-static method add(int,int): the method that needs an object.
  • cannot be referenced: you can't use it here.
  • from a static context: "here" is inside a static method, usually main.

Fix 1: make the method static

If the method doesn't use any object data, only its parameters, it doesn't need an object. Mark it static:

Main.java
public class Main {
    static int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        System.out.println(add(2, 3));
    }
}
Terminal
5

This is the right fix for most small helper methods in beginner programs: calculations, conversions, printing a menu. A quick test: does the method use any variable declared outside it, in the class? If not, it can be static.

Fix 2: create an object and call the method on it

If the method works with data that belongs to an object, keep it non-static and create the object:

Main.java
public class Main {
    int count = 0;

    void increase() {
        count++;
    }

    public static void main(String[] args) {
        Main app = new Main();
        app.increase();
        app.increase();
        System.out.println(app.count);
    }
}
Terminal
2

new Main() creates an object. app.increase() answers the question which object?: this one, app. This is how real Java programs are built, and the fix to choose once your class holds data.

The variable version: non-static variable cannot be referenced

The same rule applies to variables declared in the class, outside any method:

java
public class Main {
    int count = 0;

    public static void main(String[] args) {
        count++;
        System.out.println(count);
    }
}
Terminal
Main.java:5: error: non-static variable count cannot be referenced from a static context
        count++;
        ^
Main.java:6: error: non-static variable count cannot be referenced from a static context
        System.out.println(count);
                           ^
2 errors

Each object would have its own count, and main has no object. The fixes are the same: make it static int count = 0; if there should be one shared count, or create an object and use app.count.

A very common case is a Scanner declared in the class instead of inside main:

java
import java.util.Scanner;
public class Main {
    Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        String line = scanner.nextLine();
    }
}
Terminal
Main.java:5: error: non-static variable scanner cannot be referenced from a static context
        String line = scanner.nextLine();
                      ^
1 error

Move the Scanner into main, or make it static Scanner scanner = ....

Calling an object's method on the class name

The error also appears when you call a non-static method through the class name instead of an object. Suppose Account.java has a normal deposit method:

Account.java
public class Account {
    private int balance = 0;
    void deposit(int amount) {
        balance += amount;
    }
    int getBalance() { return balance; }
}

And Main.java calls it like this:

java
Account acc = new Account();
Account.deposit(50);
Terminal
Main.java:4: error: non-static method deposit(int) cannot be referenced from a static context
        Account.deposit(50);
               ^
1 error

You created the object acc but called the method on Account, the class. Java reads Account.deposit as a static call. Use the object, with a lowercase name:

java
acc.deposit(50);
System.out.println(acc.getBalance());
Terminal
50

This is why Java naming conventions matter: class names start with a capital letter (Account), variables with a small one (acc). When you see a capital letter before the dot, you're calling something on the class.

The "this" version

this means "the current object". In a static method there is no current object, so:

Terminal
Main.java:4: error: non-static variable this cannot be referenced from a static context
        System.out.println(this.name);
                           ^
1 error

The same message appears in a less obvious place: creating an object of a class that's written inside Main without static:

java
public class Main {
    class Dog { }
    public static void main(String[] args) {
        Dog d = new Dog();
    }
}
Terminal
Main.java:4: error: non-static variable this cannot be referenced from a static context
        Dog d = new Dog();
                ^
1 error

An inner class without static belongs to a Main object. The simplest fix for beginners: move Dog into its own file, Dog.java, or declare it static class Dog { }.

How the message looks in Eclipse

Eclipse words the same problem differently:

Editor
Cannot make a static reference to the non-static method add(int, int) from the type Main
Cannot make a static reference to the non-static field count

Same cause, same two fixes. IntelliJ IDEA shows the javac wording in its build output, with a java: prefix.

Which fix should you choose?

Ask one question: does this method or variable describe one particular object?

  • No: it's a general helper, like a calculation, a conversion or a menu. Make it static.
  • Yes: it's an account's balance, a player's score, a dog's name. Keep it non-static, create objects with new, and call the method on each object.

Making everything static makes the error disappear, and works in small programs. But it quietly turns every value into one shared value. If you later have two accounts, they'd share one balance. When the data belongs to "a thing", give it an object.

Errors that look alike

  • cannot find symbol: the method doesn't exist, or is misspelled. See cannot find symbol.
  • Cannot invoke "..." because "..." is null: you have a variable for the object, but it was never given one with new. See NullPointerException.
  • method X in class Y cannot be applied to given types: the method exists and can be called, but the arguments don't match.

Wrong fixes that don't help

  • Removing static from main. It compiles, but in our test with Java 21, java Main then refused to start: Error: Main method is not static in class Main, please define the main method as: public static void main(String[] args).
  • Adding static to everything, including data in classes like Account. All accounts would share one balance.
  • Creating a new object inside every call, like new Account().deposit(50). It compiles, but the deposit goes into an object you throw away immediately.

A debugging walkthrough

javac reports non-static method calculateTotal() cannot be referenced from a static context:

  1. Find the call. It's inside main or another static method.
  2. Open calculateTotal. Does it use any variables declared in the class (outside methods)?
  3. If not, add static to it. Done.
  4. If yes, those variables belong to an object. In main, create one with new and call obj.calculateTotal().
  5. Check the name before the dot. A capital letter means the class; you probably want your object variable.

Practice

Predict whether each compiles.

  1. A static void main that calls static void printMenu().
  2. A static void main that reads int lives = 3; declared in the class without static.
  3. Player p = new Player(); p.jump();, where jump is non-static.

Answers. (1) Compiles: static calling static. (2) non-static variable lives cannot be referenced from a static context. (3) Compiles: the method is called on an object.

Frequently asked questions

What does "non-static method cannot be referenced from a static context" mean?

You called a method that belongs to an object from a static method like main, where there's no object. Make the method static, or create an object and call it on that object.

Why is main static in Java?

Java needs to run main before any objects exist, so main belongs to the class, not to an object.

Should I make all my methods static?

No. Static is right for general helpers. Methods that work with an object's data should stay non-static and be called on objects.

What is a static context?

Any place that belongs to the class rather than an object: a static method like main, or a static block. There's no this there.

How do I call a non-static method from main?

Create an object with new, then call the method on it: Main app = new Main(); app.run();.

A quick checklist

  1. Find the call: it's inside a static method, usually main.
  2. Does the method or variable use an object's data?
  3. No: add static.
  4. Yes: create an object with new and use object.method().
  5. Check the name before the dot: object variable, not class name.

For a method that works on any error, read how to read an error message. The official Java tutorial on class members explains static and instance members.

1 beginner error a week

One common error, what it means and how to fix it, in a short email. Free, and you can unsubscribe in any email.

Java

Java error: missing return statement, and how to fix it

Why Java says error: missing return statement when your method clearly returns: if without else, a loop that may not return, try/catch and switch. With compiled examples and fixes.

Error explained · · 9 min read

Learn by writing the code yourself.

Twenty minutes with the tutor, right in your browser. No card, nothing to install.

Start the free trial
A session: the tutor's chat on the left, the code editor with a Java program and its output on the right