1

I am writing a code in 2 separate classes that takes a temp as 2 separate variables and prints it out and then runs conversion methods to get temps in other scales. I keep getting the error non-static variable value cannot be referenced from a static context in the Temperature.java part of my code because of the variables being in the constructor which is not static. The whole concept is a bit confusing to me and I'd love any input on hope static and non-static works and how you can switch between them without issue.

code as follows:

public class Temperature
{
  int value = 50;
  String scale = "F";

   public Temperature(int value, String scale){
    value = value;
    scale = scale;
   }

  public static void value(int value){
    int number;
  }
  public static boolean scale(String scale){
    if (scale == "C"){
      return true;
    }
    else if (scale == "F"){
      return true;
    }
    else if (scale == "K"){
      return true;
    }
    else{
      return false;
    }
  }

  public static void convertToCelsius(int value, String scale){
    if (scale == "F"){
      int newValue = (5/9) * (value - 32);
      System.out.println("The equivalent tempurature is " + newValue + "C");
    }
    else if (scale == "K"){
      double newValue = value - 273.15;
      System.out.println("The equivalent tempurature is " + newValue + "C");
    }
    else{
    }
  }

   public static void convertToFarenheit(int Value, String scale){
    if (scale == "C"){
      int newValue = ((9/5) * (value + 32));
      System.out.println("The equivalent tempurature is " + newValue + "F");
    }
    else if (scale == "K"){
      int newValue = ((9/5) * (value - 273) + 32);
      System.out.println("The equivalent tempurature is " + newValue + "F");
    }
    else{
    }
  }

    public static void convertToKelvin(int value, String scale){
    if (scale == "F"){
      int newValue = ((5/9) * (value - 32) + 273);
      System.out.println("The equivalent tempurature is " + newValue + "C");
    }
    if (scale == "C"){
      double newValue = (value + 273.15);
      System.out.println("The equivalent tempurature is " + newValue + "C");
    }
    else{
    }
  }      
  }

for the main method the code is in the separate class as follows:

public class UsingTemperature
{
 public static void main(String[] args)
 {
  Temperature t = new Temperature(50, "F");//declaring an object
  System.out.println(t);

  // Test out conversions:
  System.out.println("Test out conversions:");
  // F to C
  t.convertToCelsius();
  System.out.println(t);

  // C to F
  t.convertToFahrenheit();
  System.out.println(t);

  // F to K
  t.convertToKelvin();
  System.out.println(t);

  // K to F
  t.convertToFahrenheit();
  System.out.println(t);

  // F to K:
  t.convertToKelvin();
  System.out.println(t);

  // K to C
  t.convertToCelsius();
  System.out.println(t);

  // C to K
  t.convertToKelvin();
  System.out.println(t); 
 }

}
  • also see: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – jmj Jul 30 '14 at 18:56
  • Static refers to something that be accessed outside of a class without the creation of an object. – jhobbie Jul 30 '14 at 18:57
  • Thank you jhobbie! That is a great clear and simple way of putting it – user3889083 Jul 30 '14 at 19:03
  • btw you have a ton of integer division issues here – Kevin L Jul 30 '14 at 19:09
  • 1
    Did you try searching for ["non-static variable cannot be referenced from a static context"](http://stackoverflow.com/search?q=%22non-static+variable+cannot+be+referenced+from+a+static+context%22+is%3Aquestion) on this site? There are a number of possible duplicates. – Joshua Taylor Jul 30 '14 at 19:12

2 Answers2

0

t is an object, but all of your convertToScale methods are static. Use Temperature.convertToScale(t.value, t.scale)

Also,

t.convertToCelsius();
System.out.println(t);

probably does not do what you expect it to do. None of your convertToScale methods are mutators (they just print stuff), so calling t.convertToCelsius() only prints out what the conversion does, then System.out.println(t) would print the default object.toString() (which gives you @0012378 or something)

Also, I just noticed all of the (5/9) and (9/5) you have, which will almost certainly break the math as given, due to how integer division works. Explicitly declare one of them as a double with 5.0 or 5d

Kevin L
  • 1,066
  • 3
  • 12
  • 21
0

When you use the Static Modifier in your methods, these methods will exist independently of any instances created for the class. So in your class Temperature your static methods:

  • Exist before you make a new instance of Temperature
  • There is just one copy of your static methods, regardless of the number of instances of Temperature you have.

Besides, I think that you only want Temperature as a utility, so the constructor makes no sense because a constructor is used only to create instances

When you need to use methods from Temperature: Temperature.name_method(params)

  • That is actually a great point. I had been suggested to use a constructor but it seems maybe without would simplify things a lot. Thank you! – user3889083 Jul 30 '14 at 21:21