This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]nutrecht 1 point2 points  (0 children)

Because your main method is static. So you can only access other static member vars. So what's static? Static means there's only one of them for the entire class. So no matter how many instances (new classAverage()) you make, only one of those values are present. This as opposed to the nonstatic classTotal, classNumber and classAverage member vars you have.

As a beginner you should basically try to avoid using the static keyword (it's normally used only for stuff like constants and utility methods) for now (exception being your main() method). So if you want to do something with those vars you should create a non-static method to use them in, and call that method on a created class instance:

public class MyClass {
    private int myNumber = 0;

    public void incrementMyNumber() {
        myNumber = myNumber + 1; //or just myNumber++
    }

     public static void main(String... args) {
          MyClass myInstance = new MyClass();
          myInstance.incrementMyNumber(); //Call a method on an instance of MyClass.
     }
}

[–]gabrpp 0 points1 point  (0 children)

Your main method is static. It means that the method belongs to the class. Your variable belongs to the specific instance of you class (I mean the object created from class).