Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Multiplication of two integers not working

The compiler tells me there is an error on line 12 (int a = calcArea(7, 12);). The error is "cannot find symbol". Can someone tell me what the problem is?

class area {

   int calcArea(int height, int width) {

        return height * width;
    }
}

class areaTest {
    public static void main(String[] args) {

            int a = calcArea(7, 12);
    }
}

5 Answers

Sorry my bad.

We also need to create a new object of class area before invoking it's class function. Try the code below, it should work.

class area {

   public int calcArea(int height, int width) {

        return height * width;
    }
}

class areaTest {
    public static void main(String[] args) {

           area a = new area();
            int x = a.calcArea(7, 12);
    }
}

Hi Matthias,

class area {

   public int calcArea(int height, int width) {

        return height * width;
    }
}

You have missed the public keyword before the function calcArea which makes it private, hence you couldn't call it from outside.

Hope that helps. All the best.

Hi. Thanks. Adding the public keyword still compiles the same error.

Thank you. This code gives me the desired result :)

class area {

   int calcArea(int height, int width) {

        return height * width;
    }
}

class areaTest {
    public static void main(String[] args) {

            area a = new area();
            int x = a.calcArea(7, 12);
            System.out.println(x);
    }
}

There is two ways to call a method in java. You should make your method calArea a static method so you can call it a the static method main. You can call it with the instance of areaTest Class: new areaTest().calcArea(); It should work. Good Luck ;)