I asked this question here (thinking I would help people) Creating an unnecessary getter and unearthed a huge area of ignorance I have.
In this answer it was pointed out to me I had a fatal flaw in my code and I quote for ease:
"This is wrong:
public Patient(final String ptNo, final String ptName,
final String procDate, final int procType, final String injury,
final String drName) throws IOException
{
Patient.ptNo = getPtNo();
Patient.ptName = getPtName();
Patient.procDate = getProcDate();
Patient.procType = getProcType();
Patient.injury = getPtNotes();
Patient.drName = getDrName();
}
As you're completely ignoring all values passed in as parameters. Instead do:
public Patient(final String ptNo, final String ptName,
final String procDate, final int procType, final String injury,
final String drName) throws IOException
{
Patient.ptNo = ptNo;
Patient.ptName = ptName;
Patient.procDate = procDate;
Patient.procType = procType;
Patient.injury = injury;
Patient.drName = drName;
}
Where here you're setting your class's fields with the parameter values."
What I don't understand, is why the values are being ignored. I call separate methods for eg:
public static String getPtName()
{
System.out.print("Enter patient name: \n");
try
{
ptName = stdin.readLine();
} catch (IOException e)
{
System.out.println("Error! Enter a valid option.");
getPtName();
}
return ptName;
}
So I thought that was the same, in a longer way of writing the second block of code.
Can someone please explain to me, why it is different?
edit The assignment requirements from uni.
C) Provide a constructor for the class that accepts the patient number (a String), patient name (a String), procedure date (a String in the format dd/mm/yy), procedure type (an int), the injury description (a String) and the doctor name of the physician who is administering the patient’s treatment.
This constructor should initialise the instance variables with the corresponding parameter values that have been passed in - it should also initialise the patient notes instance variable to the injury description that was passed in initially and initialise the patient status instance variable to ‘S’ (indicating that the new patient has had a procedure Scheduled).
public Patient (String patientNo, String patientName, String procedureDate, int procedureType, String injuryDescription, String doctorName)D) Implement accessors for the patient number, patient name, procedure date, patient notes and doctor name instance variables.
ptNamenot ask by input keyboard, that's the point. 2nd point you are setting properties to static variables in that way in your constructor, not even in your instance that create! - nachokk"... eclipse throws up errors and that is why it has developed to be static, but that could be because there's something wrong with my code elsewhere ..."-- then you're fixing things backwards. The solution is not to make things static but to never call them in a static way. That's why all your Patient objects hold the same data. Again, nothing should be static other than the main method (with a few exceptions). - Hovercraft Full Of Eels