Non-static variable cannot be referenced...

Java main() method cannot access a non-static member of its class. That means, public static void main(String[] args) is a static method. Instance variables (variables defined in the class but not marked as static keyword) cannot be accessed from a static method without referencing an instance of the class. An attempt to use the variables and methods of the class which do not have the static modifier without creating an instance of the class is caught by the Java compiler at compile time and flagged as an error: Cannot Be Referenced From a Static Context . example
class MyCar { int tyres = 4; //non static variable public static void main(String[] args) //static method { System.out.println(tyres); } }

When you run this program, you will get:


Java Non-static variable cannot be referenced from a static context
Here "tyres" is an instance variable and you call it from a static method (public static void main()). A static variables or static methods is common to all the instances (or objects) of the class because it is a class level variable. These Class level variables or methods gets loaded prior to instance level methods or variables. So if you try to access an instance variable (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. That's why the above code is giving you the error Non-static variable cannot be referenced from a static context.

How to access non-static variable inside static method ?

To solve your problem, you need to create an instance of your class and then you can access the methods and variables of the class that have not been declared with the static modifier .
class MyCar { int tyres = 4; public static void main(String[] args) { MyCar mc = new MyCar(); //create instance of your class System.out.println(mc.tyres); } }
The static keyword modifies the lifecycle of a method or variable within a class. A static method or variable is created at the time a class is loaded. A method or variable that is not declared as static is created only when the class is instantiated as an object by using the new operator.

Another approach


Java non-static variable
First you must understand the basic thing. A non-static variable (instance variable) only exists when there is an instance of an object . A static method/variable belongs to the class itself and a non-static (aka an object instance) method belongs to each object that is generated from that class. You can access member variable of a class directly only by making it as static . If you change the "tyres" variable as static, the above program will run without error.
class MyCar { static int tyres = 4; //changed to static public static void main(String[] args) { System.out.println(tyres); //you can directly access static variable here } }

Static methods are useful if you have only one instance where you're going to use the method, and you don't need multiple copies (objects). Non-static methods are used if you're going to use your method to create multiple copies.

  1. static: no need to create object we can directly call using
ClassName.methodname()
  1. non-static: we need to create an object like
ClassName obj=new ClassName() obj.methodname();