I am working on a program where i have to include two static variables. I included them in my constructor before creating an array of ten objects. Later on, I tried to implement one of the static variables in the following way:
if (finalScore >= students.get(0).minA){
finalLetterGrade = "A";
aCounter++;
//(student.get(0).minA = 90)
The program shouldve worked fine, however Ecipse isn't allowing me to save the program because of the following error message:
"The static field ClassGrade.minA should be accessed in a
static way"
The error pops up at the first line of the code that i provided. Could anyone explain to me the correct way to supposedly access a static variable of an object in java, or at the very least advise me on how to get past this error message and save\run my program?
解决方案
You get the warning because static methods should be accessed via their container class itself (Classname.staticMethod()), not by one of its instances. Change
if (finalScore >= students.get(0).minA)
to
if (finalScore >= ClassGrade.minA)