Normally we write classes with private variables and expose getters and setters to those variables which we think right approach . this is called as encapsulation in java . to protect our class properties we define those variables as private and expose public methods to modify properties . but there encapsulation not providing any security as such .
take a look at below class which has public members and we can change those members directly without using methods . even child class can access those variables modify directly .
public class Employee {
public String firstName;
public String lastName;
}
but here only the problem is we can't test these classes and also latest frameworks like spring ,struts developed on POJO definition , hence if you don't expose methods difficult to use these type of classes .
Drawbacks
- Testing can't be done
- Can't use latest frameworks
- No proper readability
- public variables are exposed and can be modified directly
Jashuva Blosch says we can create java class without any getters and setters as JIT compiler does optimization which removes the getters and setters .
Comments