2

I know this is probably a super simple question but I can't seem to figure it out for the life of me.

As the title states I just want to call the constructor in the Main Method.

class Example{
  public static void main (String[] args)
  {
   //I want to call the constructor in the mpgCalculator class....
  }

  public class mpgCalculator {
    public double compute(double mpg, double sizeOfTank)
    {
      double mpL = mpg * 4;
      double tankSizeL = sizeOfTank * 4;
      double kmpL = mpL * 1.6;
      double result = kmpL / tankSizeL;

      return result;
    }
  }
}
1
  • 1
    In Java, class names should start with a capital letter (MpgCalculator). This isn't a language requirement, but it's a universal convention, and it makes reading your code much easier. Aug 25, 2013 at 2:22

3 Answers 3

3

The class mpgCalculator only has the default constructor since you didn't define one. You don't call the constructor manually; instead, you create a new object and it's called automatically. You probably want this:

mpgCalculator calc = new mpgCalculator();

In this particular case, the compute function doesn't actually relate to any particular copy of mpgCalculator, and so you can make it static if you want (public static double compute). Then in another method you can say:

double kmpL = mpgCalculator.compute(mpg, size);
3

To add additionally to the problem; you appear to be using an inner class. You cannot instantiate the class the way you have built it without having an object of Example first.

EDIT: two examples of possible resolutions to your problem:

First, static inner class; essentially like a nested class of C++. The inner class does not need to be associated with another instance.

public class InnerClassTest {
    public static void main(String[] args) {
        InnerClass test = new InnerClass();
    }
    public static class InnerClass {
        InnerClass() {
          System.out.println("test");
        }
    }
}

Second, the complicated inner class instantiated from the outer class.

public class InnerClassTest {
    public static void main(String[] args) {
        InnerClassTest example = new InnerClassTest();
        InnerClass test = example.new InnerClass();
    }
    public class InnerClass {

        InnerClass() {
            System.out.println("test");
        }

    }
}

If you just need a utility function, then follow chrylis's answer.

1
  • Like this? class Example{ public static void main (String[] args) { } public class mpgCalculator { mpgCalculator calc = new mpgCalculator(); public double result = calc.compute(1.0,2.0); public double compute(double mpg, double sizeOfTank) { double mpL = mpg * 4; double tankSizeL = sizeOfTank * 4; double kmpL = mpL * 1.6; double result = kmpL / tankSizeL; return result; } } } God I suck at this. Aug 25, 2013 at 2:43
0

Since, mpgCalculator (which should be renamed to MpgCalculator or MPGCalculator to differentiate it from variable names) is an inner class of Example you need an outer class instance to instantiate it.

double value = new Example().new MpgCalculator().compute();

But, this seems like a utility class in which case you can have it nested by making it static.

public static class MpgCalculator {
    // ...
}

You could then create its instance without needing to create an outer class instance too.

double value = new MpgCalculator().compute();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.