0

I wanna access my teach List in java, but I get an error message: "Cannot resolve symbol 'teach'. How do I access my teach list in my allTeachers() method??? Your help would be really appreciated and if you can tell me how to fix this it would be awesome! I want to learn how to fix this problem so I know what to do in the future if I have to do something similar like this again.

Teacher class code:

package SchoolSystem;

import java.util.ArrayList;
import java.util.List;

public class teacher {
    public teacher() {
        //null
    }

    public String first_name;
    public String last_name;
    public String teacher_id;
    public String course;

    public teacher(String first_name, String last_name, String teacher_id, String course) {
        this.first_name = first_name;
        this.last_name = last_name;
        this.teacher_id = teacher_id;
        this.course = course;

        //add teachers
        List<teacher> teach = new ArrayList<>();
        teach.add(new teacher(first_name, last_name, teacher_id, course));
    }

    //return firstname
    public String getFirstName() {
        return first_name;
    }

    //return lastname
    public String getLastName() {
        return last_name;
    }

    //return teacherId
    public String getId() {
        return teacher_id;
    }

    //return course
    public String getCourse() {
        return course;
    }

    //return all teachers
    public void allTeachers() {
        System.out.println("-------------------------------------------------------------------");
        System.out.printf("%10s %20s %5s %5s", "FIRSTNAME", "LASTNAME", "ID", "COURSE");
        System.out.println();
        System.out.println("-------------------------------------------------------------------");
        for(teacher t: teach) {
            System.out.format("%10s %20s %5s %5s",
                    getFirstName(), getLastName(), getId(), getCourse());
            System.out.println();
        }
        System.out.println("-------------------------------------------------------------------");
    }
}
J.R. BEATS
  • 93
  • 6
  • 2
    a `teacher` object (which should be named `Teacher` to conform to Java naming conventions) probably shouldn't contain a list of teachers. But if you want to do it that way you will need to make `teach` a field of your class, instead of a local variable, which disappears when the method exits. – tgdavies Nov 10 '21 at 03:43
  • Should I move my list to the main file?? – J.R. BEATS Nov 10 '21 at 03:46
  • Well, ask yourself this: if you create two instances of `teacher`, which teachers will be in `teach` in the first instance, and which teachers will be in `teach` in the second instance? If you aren't sure, write a program which does this and see. – tgdavies Nov 10 '21 at 03:51
  • Please also read up on the difference between instance fields and static fields. This may help: https://stackoverflow.com/questions/20671008/what-is-the-difference-between-a-local-variable-an-instance-field-an-input-par Think about whether a static field would be a better choice for `teach` (and please rename `teach` to `teacherList`) – tgdavies Nov 10 '21 at 03:53
  • 1
    Did you solve your problem? –  Nov 10 '21 at 04:19
  • yes I did thank you for asking – J.R. BEATS Nov 10 '21 at 19:59

1 Answers1

2

Put this:

List<teacher> teach = new ArrayList<>();

inside the teacher class, not the constructor like this:

public class teacher {
    private List<teacher> teach = new ArrayList<>();
}

Then, you should be able to access it.