简体   繁体   中英

Trouble calling a method from another class

Below is my class file Rectangle and the class file testRectangle with the main method. Now in testRectangle when I call on the method "perimeter" in the class "Rectangle", I receive an error message. The error message I receive states, "Change modifier of 'perimeter()' to 'static'. The method can't be static because I will have several different rectangle objects in the main method. Am I doing something wrong? Any help would be greatly appreciated.

Rectangle.java

public class Rectangle {
    private  int length;
    private  int width;

    Rectangle(int len, int wid) {
        length = len;
        width = wid;
    }
    public int perimeter(Rectangle rec){
        int p = 2*length + 2* width;
        return p;
    }
}

testRectangle.java

    public class testRectangle {

        public static void main(String[] args) {
            Rectangle r1 = new Rectangle(5,4);
            int r1Perimeter = Rectangle.perimeter(r1);
 //the line above this is where I get the error message
 //the red squiggly line goes under "Rectangle.perimeter(r1);
        }

    }

You have to call that method on an object of Rectangle. Because, perimeter() is an instance method, so you have to call on an instance.

If perimeter() is a static method, then you can call it with Class, like Rectangle.perimeter(r1);

int r1Perimeter = r1.perimeter(r1);

And there is no need to pass the Rectangle object there, define perimeter() method like below

public int perimeter(){
   int p = 2*length + 2* width;
   return p;
}

When you call Rectangle.perimeter , where Rectangle is a class, it's only allowed if perimeter is a static method.

But the problem here is in how you defined perimeter :

public int perimeter(Rectangle rec){

You really want this to be an instance method, because you want it to work on on an instance of Rectangle . So you really don't want to include a Rectangle parameter. In fact, your code body never uses rec , so the parameter here is wasted. You should change it to

public int perimeter() {
    ...
}

and then the call Rectangle.perimeter(r1) becomes r1.perimeter() .

Whenever you call a class method, in this case perimeter() , the compiler needs to know which 'rectangle' you are referring to. If you were simply to refer to Rectangle.perimeter() , you are not specifying which rectangle, so the compiler becomes confused. Once you create a rectangle Rectangle r1 = new Rectangle(5,4) , you have to let the compiler know that you are getting the perimeter of triangle r1 . To do this you must call r1.perimeter() .

Essentially, when you define a method to a class, a pointer this to the object is passed along with it so that the compiler knows which specific Rectangle instance you are referring to.

If, on the other hand, you really wanted to call a method with Rectangle.perimeter() , then you would be referring to a method that would be the same for every rectangle. For example, you could create a method called getNumSides() , where no matter the rectangle, the value will always be the same. To do this you must declare it with static , meaning that the this pointer is no longer passed when the method is called. The method would be declared with public static int getNumSides() and would be called with Rectangle.getNumSides() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM