Guest User

Untitled

a guest
Feb 18th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. package com.systems.singleton;
  2.  
  3. /*
  4. * One thing to remember here is, when serializing an enum, field variables are not
  5. * getting serialized. For example, if we serialize and deserialize the SingletonEnum
  6. * class, we will lose the value of the int value field
  7. */
  8.  
  9. public enum SingletonEnum {
  10. INSTANCE;
  11. int value;
  12.  
  13. public int getValue() {
  14. return value;
  15. }
  16.  
  17. public void setValue(int value) {
  18. this.value = value;
  19. }
  20. }
  21.  
  22.  
  23. public class EnumDemo {
  24.  
  25. public static void main(String[] args) {
  26.  
  27. SingletonEnum singleton = SingletonEnum.INSTANCE;
  28. System.out.println(singleton.getValue());
  29. singleton.setValue(2);
  30. System.out.println(singleton.getValue());
  31.  
  32. }
  33.  
  34. }
Add Comment
Please, Sign In to add comment