Ron_ish
Ron_ish

Reputation: 111

Cannot make a static reference to the non-static method tostring() from the type Box

This is my Box.java

public class Box{ 
  private int height, width, length;

  public void setLength(int il) {
    length = il;
}
  public void setWidth(int iw) {
    width = iw;
}
  public void setHeight(int ih){
    height = ih;
}
  public int getVolume() {
    return length * width * height;
}
  public int getSurfaceArea(){
    return ((length*width)*2)+((length*height)*2)+((width*height)*2);
  }
  public String tostring(){
 return "Height is: "+"," + "Length is: "+"," + "Width is: "+"," + "Volume is: "+"," + "Surface Area is: ";
}
  public Box ( int il, int iw, int ih ) {
    length = il;
    width = iw;
    height = ih;
  }
  public Box() {}
}

And this is my BoxApp.java

public class BoxApp {
  public static void main (String[ ] args) {
    Box box1 = new Box();
    System.out.println(Box.tostring());
  }
}

when i run it gives me this error [line: 9] Error: Cannot make a static reference to the non-static method tostring() from the type Box

Upvotes: 0

Views: 6477

Answers (3)

bharath
bharath

Reputation: 14453

Why you are using BOX.toString()? Use box1.toString() Or change the toString() method as static in BOX.java.

Upvotes: 2

user655145
user655145

Reputation:

you should call box1.tostring(), not Box.tostring()

The method isn't a static class method...

Upvotes: 1

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81694

Should be

    System.out.println(box1.toString());

You want to call the method on the object you just created, not on the name of the class Box, which would be appropriate only for a static method, as the error message suggests.

Upvotes: 2

Related Questions