Reputation: 47
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?
Upvotes: 0
Views: 2192
Reputation: 7348
Just for your understanding would like to explain the context where static variables can/cannot be used.
Lets say we have a class Demo.
public class Demo {
int number;
static int counter;
public static void printCounter(){
System.out.println(number); // Shows compilation error.
System.out.println(counter);
}
public void printNumber(){
System.out.println(number);
System.out.println(counter);
}
}
Now this class has a static variable counter
and member variable number
. Similarly it has 2 methods static method printCounter()
and non-static method printNumber()
Now let's go by the definition
Member variable : variable belonging to a particular object
Static variable : variable belonging to a particular class. This means every object of that class can access that variable. Therefore variable belonging to all the objects.
Let's consider printNumber()
method first. You will invoke this method through an object of Demo class like Demo demo = new Demo()
. This method can access a member variable which belongs to that object. It can also access static variable because a static variable belongs to all the objects. Therefore static variables can be accessed inside member-functions.
Now lets consider printCounter()
method. You will access this method through class like Demo.printCounter()
. Now imagine we had earlier created 3 Demo objects and the value of number
is different for each object. So if I try to access number
in printCounter() method, which value will it access? None. Because there are multiple values. Hence member variables can't be accessed from static functions and it shows compilation error.
Upvotes: 1
Reputation:
static means that you have only one of that variable / method for the entire class. You can create an infinite amount of objects, but the value won't change. Therefore, you should access it through the class and not an instance of the class:
ClassGrade.minA
instead of students.get(0).minA
Upvotes: 1
Reputation: 17132
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)
Upvotes: 2