title: How to Create Immutable Class with List<String> Element in class
body: <p>Immutable Class with List</p>
  
  <pre><code>package com.text.immutable;
  
  import java.util.Collections;
  import java.util.List;
  
  // An immutable class Student
  public final class Student 
  { 
      final String name; 
      final int regNo; 
      final List&lt;String&gt; courses;  // want to make Immutable 
  
      public Student(String name, int regNo, List&lt;String&gt; courses) 
      { 
          this.name = name; 
          this.regNo = regNo; 
          this.courses = Collections.unmodifiableList(courses);
      } 
      public String getName() 
      { 
          return name; 
      } 
      public int getRegNo() 
      { 
          return regNo; 
      } 
  
      public List&lt;String&gt; getCourses() {
          return courses;
      }
  } 
  </code></pre>
  
  <p>Testing Immutable Class to Break Immutability </p>
  
  <pre><code>package com.text.immutable;
  
  import java.util.ArrayList;
  import java.util.List;
  
  class ImmutablityTest 
  { 
      public static void main(String args[]) 
      { 
          List&lt;String&gt; courses = new ArrayList&lt;String&gt;();
          courses.add("java");
          courses.add("spring");
          courses.add("hibernate");
          courses.add("rest");
  
          Student s = new Student("ABC", 101, courses); 
  
          System.out.println("Before Update List");
          System.out.println(s.getName()); 
          System.out.println(s.getRegNo()); 
          System.out.println(s.getCourses());
          courses.add("Hibernate"); // Able to Change which affect final OutCome
          //s.getCourses().add("SpringBoot"); // giving Exception in thread "main" java.lang.UnsupportedOperationException
  
          System.out.println("After Update List");
          System.out.println(s.getName()); 
          System.out.println(s.getRegNo()); 
          System.out.println(s.getCourses());
  
      } 
  } 
  </code></pre>
  
  <p>Output is </p>
  
  <pre><code>Before Update List
  ABC
  101
  [java, spring, hibernate, rest]
  After Update List
  ABC
  101
  [java, spring, hibernate, rest, Hibernate]
  </code></pre>
  
  <p>why and how this new Course element added into the List as its from Client Side can be added up any time so how we can fix this issue as this immutable class should not allow to modifying after once created</p>