Skip to content
Advertisement

I think I have problem with static methods. How do I fix this error?

public class ConversionTester
{
    public static void main(String[] args)
    {
        double g = gallonsToPints(4.0);
        double f = feetToInches(2.0);
        System.out.println();
        System.out.println();
    }
}

public class Conversion
{
   public double gallon;
   public double feet;

   public static double gallonsToPints(double ga)
   {
       gallon = ga;
       return gallon * 8;
   }

   public static double feetToInches(double fe)
   {
       feet = fe;
       return feet * 12;
   }

}

ConversionTester.java: Line 7: You may have forgotten to declare gallonsToPints(double) or it’s out of scope. ConversionTester.java: Line 8: You may have forgotten to declare feetToInches(double) or it’s out of scope.

Advertisement

Answer

You have two options, either will work. The first is explicitly naming the class like

double g = Conversion.gallonsToPints(4.0);
double f = Conversion.feetToInches(2.0);

The second would be to add static import(s). Before public class ConversionTester like,

static import Conversion.gallonsToPints;
static import Conversion.feetToInches;
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement