Player player1 = new Player(0, 0); // creates a player object at position (0, 0) int x = player1.getX(); // retrieves the x-coordinate of player1 System.out.println("Player 1's x-coordinate is: " + x); // prints "Player 1's x-coordinate is: 0"
import com.example.game.Player; // imports a Player class from the com.example.game package Player player2 = new Player(5, 3); // creates a player object at position (5, 3) int x = player2.getX(); // retrieves the x-coordinate of player2 System.out.println("Player 2's x-coordinate is: " + x); // prints "Player 2's x-coordinate is: 5"In this example, the getX() method is called on a Player object named player2, which was imported from a custom com.example.game package. The returned value (the x-coordinate) is stored in an integer and printed to the console.