There is enum

 public enum HQuery { GET_USER_BY_LOG_PASS("select u from User u where u.login = :login and u.password = :password"); public String value; HQuery(final String value) { this.value = value; } } 

If I refer to the value value in this way: GET_USER_BY_LOG_PASS.value will this violate the principle of encapsulation? Or does enum not apply this rule as rigidly as classes?

  • 2
    enum is a crutch and does not belong to OOP in any way. You should not use it the same way you use objects. encapsulation is not about access to fields, but about knowledge of implementation. the user of the object should not be aware of how this object is arranged inside. he should know the interface and that's it. - Mikhail Vaysman
  • @Mikhail Vaysman "encapsulation is not about access to fields, but about knowledge of implementation" and access to fields does not apply to knowledge of implementation? - Pavel
  • if you do not have access to the fields, but use the knowledge of the implementation, then the principle of encapsulation will be violated. and if you have several getters that are part of the interface, this may not be a violation of the principle. In my opinion, the enum presented is a bad design and I would not do that. - Mikhail Vaysman
  • @Mikhail Vaysman what exactly do you mean by realization? I did not understand from your words direct access to the fields to the implementation is true or not? - Pavel

0