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 22, 2018 in Java by sharth
• 3,370 points
14,080 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 22, 2018 by Parth
• 4,640 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 28, 2019 by anonymous

Related Questions In Java

+3 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,520 points
249,574 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,370 points
1,163 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
835 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,920 points
1,054 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 29, 2020 in Java by Pistle
• 1,000 points
2,114 views
+9 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,620 points
463,202 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,920 points
5,858 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,890 points
42,365 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 29, 2018 in Java by Parth
• 4,640 points
741 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,640 points
895 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP