0

Newbie learning a first REST API example in Springboot. I have this snippet in my implementation class giving the error ""cannot make a static reference to a the non-static method getId() from the type Course". The getId() method is not static but the invocation appears to be from a static method leading to this error...im confused. Appreciate any help.

@Override
public Course getCourse(long courseId) {
    Course c=null;
    for (Course course:list)
    {
        if(Course.getId()==courseId)
        {
            c=course;
            break;
        }
    }
    return c;
}

My controller class:

@GetMapping("/courses/{courseId}")
public Course getCourse(@PathVariable String courseId)
{
    return this.courseService.getCourse(Long.parseLong(courseId));
}

My service class:

public interface CourseService {

    public List<Course> getCourses();   
    public Course getCourse(long courseId);
    
}

My entity class:

public class Course {

    private long id;
    private String title;
    private String description;
    public Course(long id, String title, String description) {
        super();
        this.id = id;
        this.title = title;
        this.description = description;
    }
    public long getId() {
        return id;
}

Was expecting this will compile.

user207421
  • 305,947
  • 44
  • 307
  • 483

0 Answers0