0

I used getter and setter method in Subject, and Subject is used in another class Student to instance, but this part generates an error. It seems that the class Student cannot access the setter method because it cannot instantiate the class Subject. If not, I don't know why. And I don't know how to solve it.

package school;

public class Subject {
    String subjectName;
    int score;

    public String getSubjectName() {
        return subjectName;
    }

    public void setSubjectName(String subjectName) {
        this.subjectName = subjectName;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
  
}
package school;

public class Student {
    int studentID;
    String studentName;
    Subject korea;
    Subject math;

    public Student(int id, String name){
        studentID = id;
        studentName =name;
        korea = new Subject();
        math = new Subject();                 
    }
    public void setKorea(String name, int score){
        korea.setSubjectName*(name);       //this part error
        korea.setScore(score);             //this part error
    }

    public void setMath(String name, int score){
        math.setSubjectName(name);         //this part error
        math.setScore(score);              //this part error
    }
    public  void  showStudentInfo() {
        int total = korea.getScore() + math.getScore();    //this part error
        System.out.println("학생의 총점은"+ total+"입니다.");
    }
}
  • What *exactly* is the error message? If the declaration of `Subject korea;` doesn't cause a compilation error then presumably it does know about a class called `Subject`. – Jon Skeet Aug 19 '21 at 07:53
  • maybe you forgot to re-compile the subject class after adding the methods, – Stultuske Aug 19 '21 at 07:54
  • 2
    I've just compiled this exact code, just removing the `*` in `korea.setSubjectName*(name);`, and it compiled with no problems. So it may well be an environmental problem. Please tell us how you're compiling, and how your files are laid out. (I'd also suggest removing the `spring` tag as it appears to be irrelevant here.) – Jon Skeet Aug 19 '21 at 07:54
  • Check out [What does a “Cannot find symbol” or “Cannot resolve symbol” error mean?](https://stackoverflow.com/q/25706216/3890632) – khelwood Aug 19 '21 at 07:55
  • I read the answers and solved it. thank you – Joe-Black Aug 19 '21 at 09:11

0 Answers0