How do I prevent the modification of a private field in a class?

前端 未结 10 1761
Happy的楠姐
Happy的楠姐 2020-12-22 16:45

Imagine that I have this class:

public class Test
{
  private String[] arr = new String[]{\"1\",\"2\"};    

  public String[] getArr() 
  {
    return arr;
         


        
相关标签:
10条回答
  • 2020-12-22 17:30

    You can also use ImmutableList which should be better than the standard unmodifiableList. The class is part of Guava libraries that was create by Google.

    Here is the description:

    Unlike Collections.unmodifiableList(java.util.List), which is a view of a separate collection that can still change, an instance of ImmutableList contains its own private data and will never change

    Here is a simple example of how to use it:

    public class Test
    {
      private String[] arr = new String[]{"1","2"};    
    
      public ImmutableList<String> getArr() 
      {
        return ImmutableList.copyOf(arr);
      }
    }
    
    0 讨论(0)
  • 2020-12-22 17:37

    You must return a copy of your array.

    public String[] getArr() {
      return arr == null ? null : Arrays.copyOf(arr, arr.length);
    }
    
    0 讨论(0)
  • 2020-12-22 17:40

    Returning an unmodifiable list is a good idea. But a list that is made unmodifiable during the call to the getter method can still be changed by the class, or classes that are derived from the class.

    Instead you should make it clear to anybody that extends the class that the list should not be modified.

    So in your example it could lead to the following code:

    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    
    public class Test {
        public static final List<String> STRINGS =
            Collections.unmodifiableList(
                Arrays.asList("1", "2"));
    
        public final List<String> getStrings() {
            return STRINGS;
        }
    }
    

    In the above example I've made the STRINGS field public, in principle you could do away with the method call, as the values are already known.

    You could also assign the strings to a private final List<String> field made unmodifiable during construction of the class instance. Using a constant or instantiation arguments (of the constructor) depends on the design of the class.

    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    
    public class Test {
        private final List<String> strings;
    
        public Test(final String ... strings) {
            this.strings = Collections.unmodifiableList(Arrays
                    .asList(strings));
        }
    
        public final List<String> getStrings() {
            return strings;
        }
    }
    
    0 讨论(0)
  • 2020-12-22 17:41

    at this point of view you should use system array copy:

    public String[] getArr() {
       if (arr != null) {
          String[] arrcpy = new String[arr.length];
          System.arraycopy(arr, 0, arrcpy, 0, arr.length);
          return arrcpy;
       } else
          return null;
       }
    }
    
    0 讨论(0)
提交回复
热议问题