Why non-static variable cannot be referenced from a static context? Example (original) (raw)

"non-static variable cannot be referenced from a static context" is the biggest nemesis of someone who has just started programming and that too in Java. Since the main method in java is the most popular method among all beginners and they try to put program code there they face "non-static variable cannot be referenced from a static context" compiler errorwhen they try to access a non-static member variable inside the main in Java which is static. if you want to know why the main is declared static in Java see the link.

public class StaticTest {

private int count=0;

public static void main(String args[]) throws IOException {

count++; //compiler error: non-static variable count cannot be referenced from a static context

}

}

Why the non-static variable can not be called from static method

non-static variable cannot be referenced from a static contextNow before finding the answer of compiler error "non-static variable cannot be referenced from a static context", let's have a quick revision of static concept or in particular static variable in Java.

The static variable in Java belongs to Class and its value remains the same for all instances.

static variable initialized when class is loaded into JVM on the other hand instance variable has a different value for each instance and they get created when an instance of an object is created either by using the new() operator or using reflection like Class.newInstance().

So if you try to access a non-static variable without any instance compiler will complain because those variables are not yet created and they don't have any existence until an instance is created and they are associated with any instance.

So in my opinion, the only reason which makes sense to disallow non-static or instance variable inside static context is the non-existence of instance.

In Summary, since code in static context can be run even without creating an instance of a class, it does not make sense asking value for a specific instance which is not yet created.

If you are curious, you can learn more about static variable and method by joining a good Java course like this Java Masterclass: 130+ Hours of Expert Lessons, which covers all essential Java concepts in depth.

How to access non-static variable inside static method or block

You can still access any non-static variable inside any static method or block by creating an instance of class in Java and using that instance to reference instance variable. This is the only legitimate way to access non static variable on static context.

here is a code example of accessing non static variable inside static context:

public class StaticTest {

private int count=0;

public static void main(String args[]) throws IOException {

StaticTest test = new StaticTest(); //accessing static variable by creating an instance of class

test.count++;

}

}

So next time if you get compiler error “non-static variable cannot be referenced from a static context access static member by creating an instance of Class. Let me know if you find any other reason why non-static variables cannot be referenced from a static context.

Also here is a nice summary of why non-static variable cannot be access from a static context in Java:

Other Java Tutorials you may find useful: