asadullah-ilyas

Untitled

Aug 24th, 2016
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. // Suppose we have a class "User" that saves data to Sqlite database.
  2. // According to my understanding, Sqlite doesn't save boolean type. So we save integer (1 = true  0 = false)
  3. // Now if the property "isUserAGoat" is public, any class can change its value to any integer,
  4. // whereas it is supposed to be only 1 or 0
  5. class User {
  6.   public int isUserAGoat;
  7. }
  8.  
  9. // 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
  10. class User {
  11.   private int isUserAGoat;
  12.  
  13.   public boolean isUserAGoat() {
  14.     return isUserAGoat == 1;
  15.   }
  16.  
  17.   public void setUserAGoat(boolean yes) {
  18.     isUserAGoat = yes ? 1 : 0;
  19.   }
  20. }
Add Comment
Please, Sign In to add comment