-4

I am trying to run a method which will sum the values from two objects. I'am getting "cannot make a static reference to a non-static method". Here is my code...

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

        Circle obj1 = new Circle(10);
        CircleTwo obj2 = new CircleTwo(20);

        System.out.println(calculateRadiusSum(obj1, obj2));
    }

    public int calculateRadiusSum(Circle r1, CircleTwo r2) {
        int R = r1.radius + r2.radius;
        return R;
    }

}

public class Circle {
    int radius;

    public Circle(int r) {
        r = radius;
    }
}

public class CircleTwo {
    int radius;

    public CircleTwo(int r) {
        r = radius;
    }

}
  • 2
    `calculateRadiusSum` is an instance method. Which instance do you expect it to run against? (Read the error message carefully, and make sure you understand the difference between instance and static methods.) – Jon Skeet Jan 06 '15 at 15:38
  • 1
    I couldn't agree more. The error messages you get back are so comprehensive these days. Why do folk ignore them? – Bathsheba Jan 06 '15 at 15:39

1 Answers1

1

calculateRadiusSum needs to be static too since it's not called from an instance of Main:

Change the prototype to

public static int calculateRadiusSum

Bathsheba
  • 231,907
  • 34
  • 361
  • 483