Post

JavaJava

K
Kusum Agarwal

Posted on 21st May 2022|86 views

1
votes

Why A Non-static Variable Cannot Be Referenced From A Static Context?

While I’m working on a Java program, I got this compiler error: non-static variable count can not be referenced from a static context. Can anyone tell me What does it mean, why this error occurs, and How to fix this error?

Answers
P
Dikshit Jain

Posted on 21st May 2022

Hey Kusum,
 

The non-static variable cannot be referenced from a static context is compiler error that occurs when the user tries to put program code to access a non-static variable inside main in Java that is static.

public class Static Test{
	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
	}
}

 

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo
  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos