Non-static variable cannot be referenced from a static context

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?

15 Answers
15

Leave a Comment