Encapsulation and Java Access Modifiers

9

A very simple question:

Given this class:

public class User {

  private String nome;

  //get/set

  public boolean fazQualquerCoisa(){
    **duvida**.equals("algumacoisa");
 }
}
Within the method Does AnyCase () , should I access the name directly or using the get method?

nome.equals ou getNome().equals ?

    
asked by anonymous 05.05.2015 / 16:36

1 answer

9

Depends heavily on what your fazQualquerCoisa() method does and how the getter works for your String, if inside the getter it only has a return nome; so if there is any specific handling of your String that must be done before it is called within the method, then access must be done through the getter .

As we can see in this topic: Getters and setters are an illusion of encapsulation? , people often do the getters and setters only and exclusively because they are accustomed to do so in fact are not encapsulating anything, if that is the case, fits in the first case I mentioned Whatever.

    
05.05.2015 / 16:42