+ 1

Non-static variable cannot be referenced from a static context

Why does this error occur? https://code.sololearn.com/c2NBiNnNjtO3/?ref=app

15th Oct 2019, 3:39 PM
Sanzid Sadman
Sanzid Sadman - avatar
1 Antwort
+ 5
If you want the members of your Data class to be static and access s, the Scanner object must be declared static too. The problem is that statics can exist without an object of the class, non-statics need an object, so you can't mix the context (statics don't know the object to which the non-static - in your code the variable s - belongs). Long story short: add the static keyword to the scanner object. A better solution may be to have all members non-static and to instantiate the class. I think that would better suit your program.
15th Oct 2019, 4:17 PM
Tashi N
Tashi N - avatar