0

in the class Student here what does the keyword this used in the functions refer to? what does it return exactly? is it only used in java or c/c++ also? is there any difference if used in any other language?

class Student
{
private String name;
private String section;
public static Comparator BY_NAME = new ByName();
public static Comparator BY_SECTION = new BySection();

public void setName(String name) {
    this.name = name;
}

public void setSection(String section) {
    this.section = section;
}

public String getName()
{
    return this.name;
}

public String getSection()
{
    return this.section;
}

private static class ByName implements Comparator
{
    public int compare(Object s1, Object s2)
    {
        return ((Student)s1).name.compareTo(((Student)s2).name);
    }
}

private static class BySection implements Comparator
{
    public int compare(Object s1, Object s2)
    {
        return ((Student)s1).section.compareTo(((Student)s2).section);
    }
}
}
Karan Kaushal
  • 25
  • 1
  • 1
  • 8

1 Answers1

0

In Java and c++ "this" is referring to the object's variable and NOT the class's variable.

Johan
  • 475
  • 4
  • 17