Non-static variable cannot be referenced from a static context Explain

+1 vote

class Program
{
    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 can my class variables get recognized?

May 21, 2018 in Java by sharth
• 3,350 points
10,720 views

2 answers to this question.

+1 vote

Static fields and methods are connected to the class itself and not its instances. If you have a class A, a 'normal' method b, and a static method c, and you make an instance an 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. You main could look like this then:

class Program {
  public static void main(String[] args){
    Program programm = new Program();
    programm.start();
  }
  public void start(){
    // can now access non-static fields
  }
}
answered May 21, 2018 by Parth
• 4,630 points
+1 vote
class Program
{
    int count = 0;
    public static void main(String[] args)
    {
         Program p=new Program();
         System.out.println(p.count);
    }
}
answered Sep 27, 2019 by anonymous

Related Questions In Java

+2 votes
14 answers

“A Non-static method cannot be referenced from a static context” error

Hey Techies, Non-static variables are part of the objects ...READ MORE

answered Dec 10, 2020 in Java by Roshni
• 10,420 points
182,923 views
0 votes
1 answer

Why “non-static method cannot be referenced from a static context”?

You can't call something that doesn't exist. ...READ MORE

answered Oct 17, 2018 in Java by sharth
• 3,350 points
488 views
0 votes
1 answer

Can a static reference be made to a non-static method

setLoanItem() isn't a static method, it's an instance ...READ MORE

answered Jun 26, 2018 in Java by samarth295
• 2,220 points
217 views
0 votes
2 answers

How a static map can be initialized?

public class Test { ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,890 points
283 views
0 votes
2 answers

What is a StackOverflow Error

stackoverflow error – StackOverflow Error in Java. This error is thrown to ...READ MORE

answered Aug 28, 2020 in Java by Pistle
• 1,000 points
587 views
+8 votes
16 answers

Unable to resolve this error: "javac is not recognized as an internal or external command"

Check your javac path on Windows using Windows Explorer C:\Program Files\Java\jdk1.7.0_02\bin and ...READ MORE

answered May 23, 2018 in Java by Rishabh
• 3,640 points
282,361 views
0 votes
2 answers

Connection reset : java.net.SocketException

You should check whether the client program is ...READ MORE

answered Sep 6, 2018 in Java by Sushmita
• 6,890 points
3,116 views
0 votes
4 answers

“Cannot find symbol” compilation error

The "Cannot find symbol" errors generally occur when you ...READ MORE

answered Dec 16, 2020 in Java by Gitika
• 65,870 points
32,921 views
0 votes
1 answer

parse int value from a char

Check Character.getNumericValue(char). String element = "el7"; int x = Character.getNumericValue(element.charAt(2)); System.out.println("x=" ...READ MORE

answered May 28, 2018 in Java by Parth
• 4,630 points
114 views
0 votes
1 answer

How do I fix a NullPointerException?

When you declare a reference variable (i.e. ...READ MORE

answered Apr 13, 2018 in Java by Parth
• 4,630 points
295 views