1
public class Car {

    static int model = 2005;
    static String name = "corvert";
    private String color;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    static void show() {
        System.out.println("model" + amodel + "name" + name + "color" + this.color);

    }
}

This method is not working with error non-static variable this cannot be referenced from a static context.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Qazi Ammar
  • 953
  • 1
  • 8
  • 23
  • 5
    Well given that different cars can have different colors, but in the static method you're not in the context of any one car, which color do you *expect* it to print? (My guess is that now would be a good time for you to revise what `static` means - hopefully it'll then become more obvious.) – Jon Skeet Mar 21 '16 at 17:32
  • You would want to consider changing the model and name to non static as well. If you want that to remain static, why would you make the color as non-static ? – shreshta bm Mar 21 '16 at 17:34
  • Are all cars corverts? – Clark Kent Mar 21 '16 at 17:36
  • Time to grab your Java book and re-read the chapter about `static` and what it means. – Andreas Mar 21 '16 at 17:39
  • 1
    Possible duplicate of [non-static variable cannot be referenced from a static context](http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) – RamValli Mar 21 '16 at 17:50

5 Answers5

0

You can access the public static method every where, but you only able to access to non-static method in static method via an instance of class

VinhNT
  • 1,091
  • 8
  • 13
0

I would suggest either making everything static (or local with a constructor).

When making it local the constructor may look like this:

private int model;
private String name, color;
public Car(int model, String name, String color) {
    this.model = model;
    this.name = name;
    this.color = color;
}

// setters and getters

public void show() {
    System.out.println("model" + amodel + "name" + name + "color" + this.color);
}

Now you can use Car car = new Car(2005, "Corvet", "Blue"); and then car.show() and not have to worry about static and non-static access.

Matthew Wright
  • 1,485
  • 1
  • 14
  • 38
0

Like Jon Skeet said in his comment, you may not want your method to be static, meaning there is one method for all members of the class.

If you feel the need to use a static method, you can pass it in: static void show(String color)

Laurel
  • 5,965
  • 14
  • 31
  • 57
0

Static variable you should access with class name

static void show() {
    System.out.println("model" + amodel + "name" + name + "color" + Car.color);

}
prasad
  • 1,277
  • 2
  • 16
  • 39
0

You can consider passing Car object to the static method, instead of creating a new instance of it. The "rule" is that static methods can't access instance variables and methods, but it can receive external objects and play with them.

static void show(Car car) {
        System.out.println("model" + model + "name" + name + "color" + car.color);

    }
}
The Roy
  • 2,178
  • 1
  • 17
  • 33