-1

the following code:

public class Drone {

    static int x;
    static int y;
    
    public Drone(int x, int y) {
        toString(x, y);
        System.out.println("X is "+x);
        System.out.println("Y is "+y);
        
    }
    public String toString(int x, int y) {
        System.out.println("X is "+x);
        System.out.println("Y is "+y);
        return "Drone 0 is at "+x+","+y;
    }
    
    public static void main(String[] args) {
        Drone d = new Drone(5, 3);      // create drone
        System.out.println(d.toString());   // print where is
        
    }

Makes the line System.out.println(d.toString()); seem to not print. As seen here:

console output

Hulk
  • 6,399
  • 1
  • 30
  • 52
Jordan Savage
  • 196
  • 2
  • 16

2 Answers2

0

The method toString() you invoke from main is not the public String toString(int x, int y) you've defined. Instead, it is the default implementation from Object, and that produces exactly the String you see in your output.

To add a custom implementation of toString, you need to override that implementation, which means you need to match the signature exactly (i.e. no parameters).

Something like

@Override
public String toString() {
    return "Drone 0 is at "+this.x+","+this.y;
}
Hulk
  • 6,399
  • 1
  • 30
  • 52
0

The toString method that you have written is expecting 2 arguments, which you have not passed. That's why it is calling toString of object class. If you want to call your method u need to pass the arguments.