0

I am in the very early stages of Java programming and I keep getting the compiler error mentioned in the title. Here is my main method for the code:

public class Temperature
{
  public double degrees;
  public char scale;
  public double degreesC = (5*(degrees - 32))/9;
  public double degreesF = (9*(degrees)/5) + 32;
  Temperature temp1 = new Temperature(degrees);

  public static void main(String[] args)
  {
    Temperature gTF = new Temperature();
      gTF.getTemperatureFahrenheit();
    Temperature gTC = new Temperature();
      gTC.getTemperatureCelsius();
    Temperature sD = new Temperature();
      sD.setDegrees(degrees);
    Temperature sS = new Temperature();
      sS.setScale(scale);
    Temperature sDS = new Temperature();
      sDS.setDegreesScale(degrees, scale);
    Temperature eqls = new Temperature();
      eqls.equals(temp1);
    Temperature gT = new Temperature();
      gT.greaterThan(temp1);
    Temperature lT = new Temperature();
      lT.lessThan(temp1);
    Temperature tS = new Temperature();
      tS.toString();
  }

I keep getting that compiler error whenever I try to pass in "degrees", "scale", or "temp1" into each of the method calls. Does anyone know why? Help would be greatly appreciated!

*Note: All of those methods (like getTemperatureFahrenheit, setDegrees, etc.) were provided by my professor and we are not allowed to change them, and they are all non-static.

  • A non-static variable is also known as an instance variable. You need an instance of a class, because that's where the variable is available. – Elliott Frisch Feb 05 '16 at 23:32

1 Answers1

1

Problem is main() is static where as variables such as degrees, scale,etc are non-static/instance variable which don't exist without creating instance of class. You need to instance of class Temperature in order to use it within static block, here i.e. main(), e.g. sD.setDegrees(gTC.degrees);