Why method uses class name as modifier(?) and argument?

Randy B.

Below are the 4 methods that I got confused, that 4 methods are in the Teacher class. I have a Student class and Teacher class. and in the Teacher class, declared is the ArrayList<Student> students as instance variable.

How to explain the Student that I saw in the methods given below and it is also used as a parameter. I am very confused with the Student searchStudent (in the method) and the Student student (inside the argument). Is that for ArrayList only? How to understand that concept where one class will search another class using the class name?

public Student searchStudent(Student student)
{
    //confuses me
    Student found = null;

    if (this.students.contains(student))
    {
        int index = this.students.indexOf(student);
        if (index != -1)
        {
            found = this.students.get(index);
        }
    }
    return found;
}

public Student searchStudent(int id)
{
    //confuses me
    Student beingSearched = new Student();
    beingSearched.setStudentId(id);
    return this.searchStudent(beingSearched);
}

public boolean addStudent(Student student)
{
    //confuses me
    boolean added = false;
    if (this.searchStudent(student) == null)
    {
        this.students.add(student);
        added = true;
    }
    return added;
}

public boolean addStudent(int id, String name, double grade)
{
    //this is fine as i know boolen and int, String and double//
    Student student = new Student(id, name, grade);
    return this.addStudent(student);
}
Maroun

I advice you to go through this link about Defining Methods.

  • public Student searchStudent(Student student)

    It's a public method that returns an object of type Student, it accepts an object of type Student as well. It needs to accept the student parameter because it search for it. You'll use this method when you want to search whether some student exists in your records (in the student ArrayList).

  • public Student searchStudent(int id)

    The same, but the parameter it accepts is an int. Here you'll search for the student not by the object itself, but by the id of the student.

  • public boolean addStudent(Student student)

    It's a method that adds a student (which is of type Student) to students ArrayList.

Tip: Run your code in Debug mode and follow each method you don't understand, you'll be amazed of how much this will help you to better understand the flow of the program.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why method accepts class name and name 'object' as an argument?

From Dev

Why does ArrayList class uses the name elementData as attribute and as method?

From Java

Why final argument modifier is not inherited in concrete impementation method signature?

From Dev

why can not overload no argument method ,for implicit class

From Dev

How to change Concrete class name in Argument for a derived class method

From Dev

Why there is a class name before the argument name in a function declaration?

From Dev

Why PropertyDescriptor(String, Class) constructor uses isFieldName() as the getter name?

From Dev

Why does this python class method not work? (it uses SQLite3)

From Dev

Generic class uses generic argument

From Dev

TypeError: unbound method 'method name' must be called with 'class name' instance as first argument (got str instance instead)

From Dev

ERROR: unbound method "method name" must be called with "Class Name" instance as first argument (got classobj instance instead)

From Dev

Why doesn't this instance of this class's method take this argument?

From Dev

why passing an argument to lua class method get nill

From Dev

Passing a method argument to a class

From Dev

How to create a method for an already existing class so that it uses a variable, from which it is called as an argument?

From Dev

Why class/object name must be explicitly specified for method references?

From Dev

Why can't a class method call a global function with the same name?

From Dev

Why the constructor is called if the class have a method (function) with his name?

From Dev

Why is a method with the same name as the class being called automatically?

From Dev

why does this java code not work if the method name is different from the class?

From Dev

How to use method name as an argument

From Dev

class name as argument in a public function

From Dev

Stub a method with a generic class argument

From Dev

Passing @Context argument to method in class

From Dev

Passing a class method as function argument

From Dev

Passing @Context argument to method in class

From Dev

Passing class instance in the method argument

From Dev

class and pass string as argument to method

From Dev

Optional argument in a class method with $this in PHP

Related Related

  1. 1

    Why method accepts class name and name 'object' as an argument?

  2. 2

    Why does ArrayList class uses the name elementData as attribute and as method?

  3. 3

    Why final argument modifier is not inherited in concrete impementation method signature?

  4. 4

    why can not overload no argument method ,for implicit class

  5. 5

    How to change Concrete class name in Argument for a derived class method

  6. 6

    Why there is a class name before the argument name in a function declaration?

  7. 7

    Why PropertyDescriptor(String, Class) constructor uses isFieldName() as the getter name?

  8. 8

    Why does this python class method not work? (it uses SQLite3)

  9. 9

    Generic class uses generic argument

  10. 10

    TypeError: unbound method 'method name' must be called with 'class name' instance as first argument (got str instance instead)

  11. 11

    ERROR: unbound method "method name" must be called with "Class Name" instance as first argument (got classobj instance instead)

  12. 12

    Why doesn't this instance of this class's method take this argument?

  13. 13

    why passing an argument to lua class method get nill

  14. 14

    Passing a method argument to a class

  15. 15

    How to create a method for an already existing class so that it uses a variable, from which it is called as an argument?

  16. 16

    Why class/object name must be explicitly specified for method references?

  17. 17

    Why can't a class method call a global function with the same name?

  18. 18

    Why the constructor is called if the class have a method (function) with his name?

  19. 19

    Why is a method with the same name as the class being called automatically?

  20. 20

    why does this java code not work if the method name is different from the class?

  21. 21

    How to use method name as an argument

  22. 22

    class name as argument in a public function

  23. 23

    Stub a method with a generic class argument

  24. 24

    Passing @Context argument to method in class

  25. 25

    Passing a class method as function argument

  26. 26

    Passing @Context argument to method in class

  27. 27

    Passing class instance in the method argument

  28. 28

    class and pass string as argument to method

  29. 29

    Optional argument in a class method with $this in PHP

HotTag

Archive