You write a small helper method, call it from main, and javac refuses:
Main.java:7: error: non-static method add(int,int) cannot be referenced from a static context
System.out.println(add(2, 3));
^
1 errornon-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:
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
staticbelongs to each object. Every account has its ownbalanceand its owndepositmethod that changes that balance. To use it, you need a specific object: which account? - Anything with
staticbelongs 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 astaticmethod, usuallymain.
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:
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));
}
}5This 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:
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);
}
}2new 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:
public class Main {
int count = 0;
public static void main(String[] args) {
count++;
System.out.println(count);
}
}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 errorsEach 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:
import java.util.Scanner;
public class Main {
Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
String line = scanner.nextLine();
}
}Main.java:5: error: non-static variable scanner cannot be referenced from a static context
String line = scanner.nextLine();
^
1 errorMove 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:
public class Account {
private int balance = 0;
void deposit(int amount) {
balance += amount;
}
int getBalance() { return balance; }
}And Main.java calls it like this:
Account acc = new Account();
Account.deposit(50);Main.java:4: error: non-static method deposit(int) cannot be referenced from a static context
Account.deposit(50);
^
1 errorYou 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:
acc.deposit(50);
System.out.println(acc.getBalance());50This 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:
Main.java:4: error: non-static variable this cannot be referenced from a static context
System.out.println(this.name);
^
1 errorThe same message appears in a less obvious place: creating an object of a class that's written inside Main without static:
public class Main {
class Dog { }
public static void main(String[] args) {
Dog d = new Dog();
}
}Main.java:4: error: non-static variable this cannot be referenced from a static context
Dog d = new Dog();
^
1 errorAn 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:
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 countSame 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 withnew. 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
staticfrommain. It compiles, but in our test with Java 21,java Mainthen 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
staticto everything, including data in classes likeAccount. 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:
- Find the call. It's inside
mainor anotherstaticmethod. - Open
calculateTotal. Does it use any variables declared in the class (outside methods)? - If not, add
staticto it. Done. - If yes, those variables belong to an object. In
main, create one withnewand callobj.calculateTotal(). - Check the name before the dot. A capital letter means the class; you probably want your object variable.
Practice
Predict whether each compiles.
- A
static void mainthat callsstatic void printMenu(). - A
static void mainthat readsint lives = 3;declared in the class withoutstatic. Player p = new Player(); p.jump();, wherejumpis 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
- Find the call: it's inside a
staticmethod, usuallymain. - Does the method or variable use an object's data?
- No: add
static. - Yes: create an object with
newand useobject.method(). - 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.
