Home java Error “non-static variable this cannot be referenced from a static context”

Error “non-static variable this cannot be referenced from a static context”

Author

Date

Category

I am learning to program from a textbook and tried to understand the principles of OOP. Wrote the following code in Main.java:

public class Main {
  public static void main (String [] args) {
    Dog chakki = new Dog ();
    Dog muhtar = new Dog ();
    muhtar.bark ();
    chakki.setAge (12);
    System.out.println (muhtar.getAge ());
    System.out.println (chakki.getAge ());
  }
}

And in the same directory created Dog.java and the code from it:

public class Dog {
  private static int age = 10;
  public static void bark () {
    System.out.println ("Gaf Gaf");
  }
  public static int getAge () {
    return age;
  }
  public static void setAge (int age) {
    this.age = age;
  }
}

And when I tried to compile on the command line, I get an error:

. \ Dog.java:19: error: non-static variable this cannot be referenced from a static context
        this.age = age;
      ^
1 error

Questions:

  1. Why is this happening?
  2. And when I tried to compile without methods, just changing the age value directly, then I changed the values ​​of both muhtar and chakki objects, that is, they were equal to the last value. Hence the question why the values ​​of the variables of two objects (muhtar, chakki) were equal to the last changed value. After all, I created a Dog class and declared 2 objects (muhtar, chakki), and I thought that the values ​​for these objects would be separate and different. But, apparently, I misunderstood something.

Help me figure it out and understand)))


Answer 1, authority 100%

What is the difference between static and non-static

When you declare a variable as static , it belongs to the entire class, not every object. And this variable exists in a single instance, no matter how many instances of the class.

This is the & nbsp; age of all dogs in general. You have two dogs – & nbsp; one age. Not good because dogs are of different ages.

private static int age = 10;

And this method sets the age of all dogs at once:

public static void setAge ...

And you need the age of each individual dog from the dog class:

private int age = 10;

Reasons for error “non-static variable this cannot be referenced from a static context”

Next: this.age means the value of the age variable in this particular (this ) instance of the class. But you are executing this code in a static method that belongs to the class. Since the method is class-wide, it doesn’t know any specific instances.

Therefore, the error “non-static variable this cannot be referenced from a static context” occurs. Literally: you are accessing a non-static variable, but you are doing it without respect from a static context, and it doesn’t make sense.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions