Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Suppose we have a class "User" that saves data to Sqlite database.
- // According to my understanding, Sqlite doesn't save boolean type. So we save integer (1 = true 0 = false)
- // Now if the property "isUserAGoat" is public, any class can change its value to any integer,
- // whereas it is supposed to be only 1 or 0
- class User {
- public int isUserAGoat;
- }
- // But if we make this property private and give public setter/getter, we can make sure that its value can only be 1 or 0
- class User {
- private int isUserAGoat;
- public boolean isUserAGoat() {
- return isUserAGoat == 1;
- }
- public void setUserAGoat(boolean yes) {
- isUserAGoat = yes ? 1 : 0;
- }
- }
Add Comment
Please, Sign In to add comment