¿La clase comparable es igual a la función funciona en una variable privada?

Well my code works, but I just can't understand why a private variable works in this case, inside function equals().

Or is it just a trick that if you call another object from inside the same kind of object structure then the private identifier doesn't count?

public class TestClass implements Comparable <TestClass> {
    private final String name;
    public TestClass(String name) {
        this.name = name;
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof TestClass) {
            return ((TestClass) obj).name.equals(name); //<- how does this work, isn't name private?
        } else {
            return false;
        }
    }

    @Override
    public int compareTo(TestClass test) {
        int thisValue = hashCode();
        int otherValue = test.hashCode();
        if (thisValue < otherValue) {
            return -1;
        } else if (thisValue > otherValue) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public String toString() {
        return name;
    }
}

preguntado el 15 de febrero de 14 a las 20:02

2 Respuestas

Yes, these modifiers are class definition scoped not instance scoped. Check JavaOO tutorials y Especificación del lenguaje Java:

Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

Respondido 15 Feb 14, 20:02

This is not meant for security but OO encapsulation es.wikipedia.org/wiki/… - Gertas

"Or it's just a trick if you call another Object from inside the same kind of Object structure then private identifier doesn't count?" - simply, yes.

EDITAR Explicación oficial:

"The first (left-most) modifier used lets you control what other classes have access to a member field. For the moment, consider only public and private. Other access modifiers will be discussed later. public modifier—the field is accessible from all classes. private modifier—the field is accessible only within its own class."

(http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html)

Respondido 15 Feb 14, 20:02

Can you add something substantial? Maybe a link to the JLS that describes this? - Jeroen vannevel

Here is the same answer: stackoverflow.com/questions/16354728/… Nevermind thats C# - SShabló

"The first (left-most) modifier used lets you control what other classes have access to a member field. For the moment, consider only public and private. Other access modifiers will be discussed later. public modifier—the field is accessible from all classes. private modifier—the field is accessible only within its own class." (docs.oracle.com/javase/tutorial/java/javaOO/variables.html) - Smutje

Edit it into your post so it's easier to see and you've got a decent answer :) - Jeroen vannevel

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.