0

I'm new at Java and I recently learnt about constructors in my uni class. I tried write a code to instantiate an object with arguments. The script get the inputs as arguments with Scanner instance.

The problem is, if I create a method to instantiate an object inside of it, the object would be treated as a local variable of the method. If I create a method to get arguments only and instantiate the object in the main method with them, it becomes somewhat messy. My solution is to create a method which is 1. getting inputs, 2. instantiating an object with constructor, and 3. returning the object just created. I could got a result what I've wanted but I am not sure this is acceptable or not.

Here is my code:

public class Game {
    private int gameID;
    private String gameTitle;
    private String genre;

    // constructor
    public Game(int ID, String title, String genre) {
        gameID = ID;
        gameTitle = title;
        this.genre = genre;
    }
}
import java.util.Scanner;

public class GameTest {

    static Scanner kb = new Scanner(System.in);

    public static void main(String[] args) {
        Game myGame = constructGame();
    }


    static Game constructGame() {

        int ID = kb.nextInt();
        kb.nextLine();
        String title = kb.nextLine();
        String genre = kb.nextLine();

        Game newGame = new Game(ID, title, genre);

        return newGame;
    }
}

Can this solution be a problem? Honestly, I'm bit worried about memory references things here.

Lylant
  • 21
  • 2
  • 7
  • 1
    It's not a problem, but... Why don't you have a constructor without arguments that initializes the fields to default values, and then add [setter methods](https://www.codejava.net/coding/java-getter-and-setter-tutorial-from-basics-to-best-practices) to your Employee class, and use them to assign to the Employee instance the values that the user inputs? – JustAnotherDeveloper Aug 22 '20 at 11:13
  • @JustAnotherDeveloper: Right, I prefer to use setters for this. My lecturer asked me to think how this question be able to treated with constructor instead of initializing and setting so I tried this. Thank you for the comment and the suggestion. – Lylant Aug 22 '20 at 11:28
  • 1
    1. Copying from the return value of a method does not instantiate an object. 2. The object would *not* be treated as a local variable of the method: you are confusing objects with references. Your question doesn't make sense. – user207421 Aug 22 '20 at 11:30
  • 1
    You can a Game object from createGame() without problems. The actual Game object is created on the heap, even though the reference to the object is on the stack and can be passed like any other variable. – NomadMaker Aug 24 '20 at 04:03

1 Answers1

2

This is completely acceptable. It would perhaps be cleaner if constructEmployee took the Scanner it uses as a parameter, and then the main method would pass the scanner to it, but there's nothing wrong per-se with your current code.

Mureinik
  • 297,002
  • 52
  • 306
  • 350