error: non-static method cannot be referenced from a static context (Java) — how to fix
Main.java:9: error: non-static method greet(String) cannot be referenced from a static context
greet("Ana");
^
Main.java:10: error: non-static variable count cannot be referenced from a static context
count++;
^
Quick fix: Either create an object and call through it: Main app = new Main(); app.greet("Ana");, or declare the member static if it doesn't use instance state: static void greet(String name). For non-static variable this, make the nested class static or create it from an instance.
What it means
In Java, members without static are instance members: each object has its own copy of the fields, and instance methods run on a particular object (this). A static method belongs to the class itself and runs without any object.
public class Main {
int count = 0;
void greet(String name) {
System.out.println("Hi " + name);
}
public static void main(String[] args) {
greet("Ana"); // which object's greet? there is no this
count++; // which object's count?
}
}
Because main is static, the compiler can't know which object greet or count should use.
Fix 1: create an instance
The usual fix when the class holds state: make an object and use it.
public static void main(String[] args) {
Main app = new Main();
app.greet("Ana");
app.count++;
}
A common pattern is to keep main tiny and move the program into an instance method:
public static void main(String[] args) {
new Main().run();
}
void run() {
greet("Ana"); // fine: run() is an instance method
count++;
}
Fix 2: make the member static
If the method doesn't use any instance fields (a utility or helper), it can be static:
static void greet(String name) {
System.out.println("Hi " + name);
}
The same works for a field, but be aware that a static field is shared by the whole class: every object sees the same value. Don't make fields static just to silence the error if each object should have its own.
Cause 3: calling an instance method through the class name
String s = "hello";
int n = String.length(); // length() is an instance method
List<String> list = new ArrayList<>();
ArrayList.add("x"); // add is an instance method
Fix 3
Call it on the object:
int n = s.length();
list.add("x");
Static methods are the ones you call on the class: Math.max(a, b), Integer.parseInt(s), String.valueOf(x).
Cause 4: non-static variable this cannot be referenced from a static context
You create an inner class (a nested class without static) from main:
public class Main {
class Node { int value; }
public static void main(String[] args) {
Node n = new Node(); // error: an inner class needs an enclosing Main instance
}
}
Fix 4
If Node doesn't need access to the outer object's fields, make it a static nested class:
static class Node { int value; }
Otherwise create it from an outer instance:
Main outer = new Main();
Main.Node n = outer.new Node();
Cause 5: a static field or block uses instance members
The same rule applies to static initializers and static field initializers:
int size = 10;
static int[] data = new int[size]; // error
Fix 5
Make size static (e.g. static final int SIZE = 10;), or initialize data as an instance field.
Last updated 2026-11-30.