In Java, every List holds objects, so operation list.get(i)
will return an Object
. If you want to do some action on Student
class instance, you need to check if this object is a Student
class instance., then cast it to Student
class.
If your lists holds only students, you can add a generic type to it like so: List<Student> stidents
. Then operation list.get(i)
will return a Student
instance. But this goes both ways, you can only add Student
objects to this list.
In short, you have two choices:
Use generic list type:
public static int numberPassed(List<Student> students){
...
}
or cast:
Student student = (Student)students.get(i);
System.out.println(student.mark);