Results 1 to 2 of 2
  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    2

    Default User input in constructor bad practice?

    I am making some System.out.println output only programs at the moment.

    I am making objects which always require 3 variables to be user-defined before any work is carried out using these objects.

    I have a feeling it is bad practise to include something like:

    Java Code:
    //Constructor Method
    
    System.out.println("What is variable a?");
    variableA = scanner.nextInt();
    
    System.out.println("What is variable b?");
    variableB = scanner.nextInt();
    
    System.out.println("What is variable c?");
    variableC = scanner.nextInt();
    Are my thoughts correct?

    If so, is it therefore best to just initialise the 3 variables in the constructor using 3 random (probably 0) variables and then just ensuring to write this code elsewhere?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: User input in constructor bad practice?

    The constructor is used to create an object. What usually happens is your driver class will gather all the information and pass the data as parameters to the constructor. The constructor then initialises instance variables with the data passed in. So, making the constructor do the work of gathering the data is a bad design.

Similar Threads

  1. help passing user input to constructor parameters
    By foxity.cf in forum New To Java
    Replies: 14
    Last Post: 02-02-2013, 05:19 PM
  2. User Input???
    By jonytek in forum New To Java
    Replies: 8
    Last Post: 01-13-2013, 02:52 PM
  3. Replies: 0
    Last Post: 11-03-2011, 07:33 AM
  4. Replies: 1
    Last Post: 08-30-2010, 07:08 PM
  5. User input- Pop Up Box
    By dedachi in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2009, 04:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •