Final Key Word Collection Types:
Q : final List list = new LinkedList();
//Will it Work? If it works then
list.add(23); //Will it work ? If works, then Why?
How to make the List's content Unmodified or Make the list Immutable?
Ans :
final List list = new LinkedList();
list.add(23);
Both will work. The list will allow to add the values to it.
1. Making a List or any other collection "fianl" means :"It's reference cannot be changed."
Example:
final List list = new LinkedList();
List anotherListObj = new LinkedList();
..........
list.add(someObject);
list.add(someOtherObject);
list = new LinkedList(); //Obviously not work. Error
list = anotherListObj; // Not Work. Error
2. final and immutable Objects:
3.
Q : final List list = new LinkedList();
//Will it Work? If it works then
list.add(23); //Will it work ? If works, then Why?
How to make the List's content Unmodified or Make the list Immutable?
Ans :
final List list = new LinkedList();
list.add(23);
Both will work. The list will allow to add the values to it.
1. Making a List or any other collection "fianl" means :"It's reference cannot be changed."
Example:
final List list = new LinkedList();
List anotherListObj = new LinkedList();
..........
list.add(someObject);
list.add(someOtherObject);
list = new LinkedList(); //Obviously not work. Error
list = anotherListObj; // Not Work. Error
2. final and immutable Objects:
final :
You cannot change the reference to the collection (Object). You can modify the collection / Object the reference
points to. You can still add elements to the collection
immutable :
You cannot modify the contents of the Collection /
Object the reference points to. You cannot add elements to the collection.
3.
A variable can be declared
final. A final variable may only be assigned to once. Declaring a variable
final can serve as useful documentation that its value will not change and can
help avoid programming errors.
It is a compile-time error if a
final variable is assigned to unless it is definitely unassigned immediately prior to the assignment.
A blank final is a final
variable whose declaration lacks an initializer.
Once a
final variable has been assigned, it always contains the same value. If a final
variable holds a reference to an object, then the state of the object may be
changed by operations on the object, but the variable will always refer to the
same object.
Therefore, if you wish to
enforce that the state reachable through the variable does not change, you have
to declare the variable
final, use an unmodifiable List
(for instance with Collections.unmodifiableList), and make Synapse objects immutablefinal List fixedList = Collections.unmodifiableList(someList);
Example:
package com.test.practice;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class FinalKYwordwithCollection {
public static void main(String[] args) {
final List<Integer> list = new ArrayList<Integer>();
list.add(12);
list.add(2);
list.add(null);
System.out.println(list);
list.add(4);
System.out.println(list);
List<Integer> items = Collections.unmodifiableList(list);
items.add(40);// java.lang.UnsupportedOperationException
System.out.println(list);
}
}
No comments:
Post a Comment