user3044394
user3044394

Reputation: 135

Static Suggestion. What am I doing wrong?

So I am trying to pass an ArrayList to my main method, but eclipse is telling me I need to change my arraylist to a static. I know I'm doing something wrong but I can't figure it out.

ArrayList<Patient> pList = Doctor.getPatientList();

this is the call I have in my main method.

public class Doctor {
public ArrayList<Patient> patientList = new ArrayList<Patient>();
}

public void loadPatientData() {
    BufferedReader read2 = null;

    try {   
        read2 = new BufferedReader(new FileReader("data/patient_list.txt"));
        String line;
        while ((line = read2.readLine()) != null) {
            line = read2.readLine();
            if (line == null) {
                break;
            }
            String[] lineValues = line.split(","); //split the string on this value into array
            String firstName = lineValues[0];
            String lastName = lineValues[1];
            String address = lineValues[2];
            String city = lineValues[3];
            String state = lineValues[4];
            String zip = lineValues[5];
            String ssn = lineValues[6];
            String genderNeedsConvert = lineValues[7];
            String weightNeedsDouble = lineValues[8];
            String heightNeedsDouble = lineValues[9];
            String symptomsNotReady = lineValues[10]; // these need to be broken up further (using semicolons)

            char gender = genderNeedsConvert.charAt(0);
            double weight = Double.parseDouble(weightNeedsDouble);
            double height = Double.parseDouble(heightNeedsDouble);

            Patient patient = new Patient(firstName, lastName, address, city, state, zip, ssn, gender, weight, height, symptomsNotReady); 
            patientList.add(patient); // must be of patient type.
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (read2 != null) {
            try {
                read2.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
      }
    }
public ArrayList<Patient> getPatientList() {
    return patientList;
}

This is a shortened version of my Doctor class.

public class Patient {

private String patientID;
private String firstName;
private String lastName;
private String ssn;
private char gender;
private String address;
private String city;
private String state;
private String symptoms;
private String zip;
public ArrayList<Diagnosis> diagnoses = new ArrayList<Diagnosis>();
//private Diagnosis diagnoses = new Diagnosis(0, null);// new diagnoses called as Diagnoses datatype
public ArrayList<Medication> newMedication = new ArrayList<Medication>();
//private Medication newMedication = new Medication(0, null);// newMedication called as medication datatype
ArrayList<String> symptom = new ArrayList<String>();
ArrayList<String> symptomCompare = new ArrayList<String>();
private double weight;
private double height;
int k = 0;


public Patient(String firstName,String lastName,String address,String city, String state,String zip,String ssn,char gender,double weight,double height,String symptoms){
    this.firstName = firstName;
    this.lastName = lastName;
    this.ssn = ssn;
    this.gender = gender;
    this.weight = weight;
    this.height = height;
    this.address = address;
    this.city = city;
    this.state = state;
    this.symptoms = symptoms;
    this.zip = zip;
    this.patientID = ssn.replace("-", ""); // removes dashes from ssn and sets the value to patientID
    this.patientID = this.patientID.replace(" ", ""); //removes spaces from patientID
    this.patientID = this.patientID + this.firstName.substring(0, 1) + this.lastName.substring(0, 1);


}

and above is the shortened patient class. I've been sitting here for a few hours trying different things but it keeps telling me to change the getPatientList() method to static. What am I doing wrong?

Upvotes: 0

Views: 117

Answers (5)

anacron
anacron

Reputation: 6721

In the Doctor class, the patientList, loadPatientData() and getPatientList() members are all "instance" members of the class Doctor, which means you need an instance or an object of the type Doctor.

So, to call getPatientList(), you need to create a Doctor object as below:

Doctor doc = new Doctor();
doc.getPatientList();

static members are accessed using the name of the class where as instance members are accessed using the name of the object.

Upvotes: 0

Waqar Majid
Waqar Majid

Reputation: 111

You have to declare the class object first then call its function. Like:

Doctor doc = new Doctor();
doc.getPatientList();

Else you will have to make the function static.

Upvotes: 0

user3437460
user3437460

Reputation: 17454

What ever variables/method you declare as static as known as class members. Strictly speaking, they belongs to the class instead of being an object's attribute.

When a variable is static, it exist even before the object is created. So what does that means?

  • Static methods can access static variables/methods.
  • Static methods cannot access non-static variables/methods. (because they don't exist)

If you want to let static methods access non-static variables/methods. One of the ways is to instantiate(create) the object which the method/variable you wanted to access belong to that object first.

The reason you need to instantiate first before you can access it is because you want to make them exist first. Class itself is only a blueprint, you need to create an object (to make them exist) before you can interact with it.

Example:

Doctor doctor = new Doctor();
ArrayList<Patient> list = doctor.patientList;  
//Only possible if patientList is not private

If patientList is private in Class Doctor, you need to use a getter:

Doctor doctor = new Doctor();
ArrayList<Patient> list = doctor.getPatientList();  
//getPateientList is a method in Doctor class

Upvotes: 1

Manish Maheshwari
Manish Maheshwari

Reputation: 4134

Use static modifier with public ArrayList<Patient> getPatientList():

public static ArrayList getPatientList()

You are invoking this method on class Doctor, and thus this method must be declared static.

Upvotes: 0

Eran
Eran

Reputation: 393936

Doctor.getPatientList() is the syntax for calling a static method, since Doctor is a class name.

If you want to call an instance method (and getPatientList() is currently an instance method), you should call it using an instance of a Doctor:

Doctor doctor = new Doctor();
ArrayList<Patient> pList = doctor.getPatientList();

Upvotes: 3

Related Questions