Results 1 to 2 of 2
- 10-23-2013, 12:06 AM #1Senior Member
- Join Date
- Jul 2012
- Rep Power
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:
//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();
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?
- 10-23-2013, 04:09 AM #2
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.
- By foxity.cf in forum New To JavaReplies: 14Last Post: 02-02-2013, 06:19 PM
- By jonytek in forum New To JavaReplies: 8Last Post: 01-13-2013, 03:52 PM
- By Renxx in forum New To JavaReplies: 0Last Post: 11-03-2011, 08:33 AM
- By aappee in forum New To JavaReplies: 1Last Post: 08-30-2010, 07:08 PM
- By dedachi in forum AWT / SwingReplies: 3Last Post: 03-23-2009, 05:47 AM