title: using constructor from another class in java to create objects in separate classs
body: <p>how can I use the constructor from another class in java to make an object through a method in separate class. For example below is a constructor in a player class</p>
  <pre><code>public class Player extends Entity {
  
  
  
  public Player(int maxEnergy, int x, int y) {
          this.maxEnergy = maxEnergy;
          this.energy = maxEnergy;
          carryingGhost = false;
          xPos = x;
          yPos = y;
      }
  </code></pre>
  <p>Which I want to use and create objects (player) through a method called</p>
  <pre><code>private Player createPlayer() { 
  </code></pre>
  <p>and the above method is in separate class as</p>
  <pre><code>public class GameEngine {
  
  
  **The method must return a Player object that represents the player in the 
  game.  it must set the maxEnergy for the player, and the
  X and Y positions corresponding to a tile position in the current level.    
  </code></pre>
  <p>I have tried to initialize player within method with parameters and
  without parameters as**</p>
  <pre><code>Player player = new Player(int maxEnergy, int x, int y);
  
      this.player.getEnergy();
      this.player.getMaxEnergy();
      this.player.setPosition(x, y);
      
         return player;       
      }
  </code></pre>
  <p>But it give errors.Any help will be appreciated.I am quite close to assume its not possible to have created objects like this.</p>
  <p>below I share the complete game engine which is working with other classes as well .</p>
  <pre><code>import java.awt.Point;
  import java.util.ArrayList;
  import java.util.Random;
  
  
  public enum TileType {
          WALL, FLOOR1, FLOOR2, BANK, BREACH, DOOR;
      }
  
  
  public static final int LEVEL_WIDTH = 35;
  
  public static final int LEVEL_HEIGHT = 18;
  
  private Random rng = new Random();
  
  private int levelNumber = 1;  //current level
  
     
  private int turnNumber = 1;
  
  private GameGUI gui;
  
  private TileType[][] level;
  
  private ArrayList&lt;Point&gt; spawnLocations;
  
     
  private Player player;
  
     
  private Ghost[] ghosts;
  
     
  public GameEngine(GameGUI gui) {
      this.gui = gui;
  }
  private TileType[][] generateLevel() {
      //YOUR CODE HERE
    
      return null;        //change this to return the 2D arrayof TileType 
                          //values that you create above
  }
  private ArrayList&lt;Point&gt; getSpawns() {
      ArrayList&lt;Point&gt; s = new ArrayList&lt;Point&gt;();
      // YOUR CODE HERE
      return s;
  }
     
  private Ghost[] addGhosts() {
      //YOUR CODE HERE
      
      return null;       //change this to return an array of ghost objects 
  }
  
  **/**
   * Creates a Player object in the game. The method instantiates
   * the Player class and assigns values for the energy and position.
   * The first version of this method should use fixed a fixed position 
   for  the player to start, by setting fixed X and Y values when calling 
   the constructor in the Player class. The second version of this method 
   should use the spawns ArrayLis to select a suitable location to spawn 
   the player and removes the Point from the spawns ArrayList. This will 
   prevent the Player from being added to the game inside a wall, bank or 
   breach for example. 
   
    @return A Player object representing the player in the game
   */**
  
  private Player createPlayer() {
      //YOUR CODE HERE
      
      return null;        //change this to return a Player object
  }
      
      public void movePlayerLeft() {
          
      }
  
      public void movePlayerRight() {
          
      }
  
      
      public void movePlayerUp() {
          
      }
  
     
      public void movePlayerDown() {
          
      }
  
    
      private void hitGhost(Ghost g) {
          
      }
      private void moveGhosts() {
          
      }
  
     
      private void moveGhost(Ghost g) {
          
      }
  
      
      private void cleanDefeatedGhosts() {
          
      }
  
     
      private void nextLevel() {
          
      }
  
     
      private void placePlayer() {
          
      }
  
      public void doTurn() {
          cleanDefeatedGhosts();
          moveGhosts();
          gui.updateDisplay(level, player, ghosts);
      }
  
      public void startGame() {
          level = generateLevel();
          spawnLocations = getSpawns();
          ghosts = addGhosts();
          player = createPlayer();
          gui.updateDisplay(level, player, ghosts);
      }
  }
  </code></pre>
  <p>I have used below method and its not showing error so far.</p>
  <pre><code>private Player createPlayer() {
  
    int energy=player.getEnergy();
    int maxEnergy=player.getMaxEnergy();
    int xPos=player.xPos;
    int yPos=player.yPos;
     
     return new Player(maxEnergy,xPos,yPos);       
  }
  </code></pre>