Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javatechie.interview.programming;
- import java.util.Collections;
- import java.util.Date;
- import java.util.List;
- final public class MyImmutable {
- final private int id;
- final private String name;
- final private Customer customer;
- final private List<String> list;
- final private Date date;
- public MyImmutable(int id, String name, Customer customer, List<String> list, Date date) {
- this.id = id;
- this.name = name;
- this.customer = customer;
- this.list = list;
- this.date = date;
- }
- public int getId() {
- return id;
- }
- public String getName() {
- return name;
- }
- public Customer getCustomer() throws CloneNotSupportedException {
- return (Customer) this.customer.clone();
- }
- public List<String> getList() {
- return Collections.unmodifiableList(this.list);
- }
- public Date getDate() {
- return (Date) date.clone();
- }
- public static void main(String[] args) {
- MyImmutable myImmutable = new MyImmutable(1, "javatechie", new Customer(), Collections.emptyList(), new Date());
- List<String> myList = myImmutable.list;
- myList.add("abc");
- System.out.println("Expected Exception : " + myList);
- }
- }
- package com.javatechie.interview.programming;
- public class Customer implements Cloneable {
- public Object clone() throws CloneNotSupportedException {
- return super.clone();
- }
- }
Add Comment
Please, Sign In to add comment