Categories
compiler-errors java non-static variables

Non-static variable cannot be referenced from a static context

350

I’ve written this test code:

class MyProgram
{
    int count = 0;
    public static void main(String[] args)
    {
        System.out.println(count);
    }
}

But it gives the following error:

Main.java:6: error: non-static variable count cannot be referenced from a static context
        System.out.println(count);
                           ^

How do I get my methods to recognize my class variables?

2

  • 2

    Try to avoid using static whenever possible. You can write a complete program, all static, just like in C. But it won’t be a very good one. Try to use Java the way it is meant to be used, as an object oriented language.

    Sep 15, 2015 at 16:21

  • Might help beginners – might want to check the arguments (there’s probably a mismatch)

    Jun 28 at 15:05

114

Static fields and methods are connected to the class itself and not to its instances. If you have a class A, a ‘normal’ (usually called instance) method b, and a static method c, and you make an instance a of your class A, the calls to A.c() and a.b() are valid. Method c() has no idea which instance is connected, so it cannot use non-static fields.

The solution for you is that you either make your fields static or your methods non-static. Your main could look like this then:

class Programm {

    public static void main(String[] args) {
        Programm programm = new Programm();
        programm.start();
    }

    public void start() {
        // can now access non-static fields
    }
}

    74

    The static keyword modifies the lifecycle of a method or variable within a class. A static method or variable is created at the time a class is loaded. A method or variable that is not declared as static is created only when the class is instantiated as an object for example by using the new operator.

    The lifecycle of a class, in broad terms, is:

    1. the source code for the class is written creating a template or
      pattern or stamp which can then be used to
    2. create an object with the new operator using the class to make an instance of the class as an actual object and then when done with the object
    3. destroy the object reclaiming the resources it is holding such as memory during garbage collection.

    In order to have an initial entry point for an application, Java has adopted the convention that the Java program must have a class that contains a method with an agreed upon or special name. This special method is called main(). Since the method must exist whether the class containing the main method has been instantiated or not, the main() method must be declared with the static modifier so that as soon as the class is loaded, the main() method is available.

    The result is that when you start your Java application by a command line such as java helloworld a series of actions happen. First of all a Java Virtual Machine is started up and initialized. Next the helloworld.class file containing the compiled Java code is loaded into the Java Virtual Machine. Then the Java Virtual Machine looks for a method in the helloworld class that is called main(String [] args). this method must be static so that it will exist even though the class has not actually been instantiated as an object. The Java Virtual Machine does not create an instance of the class by creating an object from the class. It just loads the class and starts execution at the main() method.

    So you need to create an instance of your class as an object and then you can access the methods and variables of the class that have not been declared with the static modifier. Once your Java program has started with the main() function you can then use any variables or methods that have the modifier of static since they exist as part of the class being loaded.

    However, those variables and methods of the class which are outside of the main() method which do not have the static modifier can not be used until an instance of the class has been created as an object within the main() method. After creating the object you can then use the variables and methods of the object. An attempt to use the variables and methods of the class which do not have the static modifier without going through an object of the class is caught by the Java compiler at compile time and flagged as an error.

    import java.io.*;
    
    class HelloWorld {
        int myInt;      // this is a class variable that is unique to each object
        static int myInt2;  // this is a class variable shared by all objects of this class
    
        static void main (String [] args) {
            // this is the main entry point for this Java application
            System.out.println ("Hello, World\n");
            myInt2 = 14;    // able to access the static int
            HelloWorld myWorld = new HelloWorld();
            myWorld.myInt = 32;   // able to access non-static through an object
        }
    }
    

    1

    • 1

      Thanks for the great explanation.

      Sep 29, 2021 at 14:49