Skip to main content

Immutable_Object & Class

** What is immutable class..??
=================================

-> Immutable class is a class which once created, it’s contents can not be changed.
-> Immutable objects are the objects whose state can not be changed once constructed.
   e.g. String class.
-> All wrapper classes in java.lang are immutable –
   String, Integer, Boolean, Character, Byte, Short, Long, Float, Double.

*** Why use immutable class..??
=====================================

-> Because java uses the concept of string literal. Suppose there are 5 reference variables,all referes to one object "sachin".
   If one reference variable changes the value of the object, it will be affected to all the reference variables.
   That is why string objects are immutable in java.

-> In case of String object, just because of SCP a single object is referred by multiple references.
   by using one reference if we are allowed to change the content then remaining refereces will be impacted.
   to overcome this problem some people made string object as immutable.
   ....
-> But in case of StringBuffer for every requirment a separate object will be created.
   by using one reference if we are allowed to change the content then remaining refereces won't be impacted.
   hence immutability concept is not required for StringBuffer object.

***How to create an immutable class???
=============================================

-> Create a final class.
   Set the values of properties using constructor only.
   Make the properties of the class final and private.
   Do not provide any setters for these properties.
   If the instance fields include references to mutable objects, don't allow those objects to be changed:
   Don't provide methods that modify the mutable objects.
   Don't share references to the mutable objects.
   Never store references to external, mutable objects passed to the constructor;
   if necessary, create copies, and store references to the copies. Similarly,
   create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.

-> Ex..

public final class FinalPersonClass {
    private final String name;
    private final int age;

    public FinalPersonClass(final String name, final int age) {     
     this.name = name;
     this.age = age;
    }
    public int getAge() {
      return age;
    }
    public String getName() {
     return name;
    }
  }

Comments

Popular posts from this blog

Mockito interview Questions

1.       Question 1. What Is Mockito? Answer : Mockito allows creation of mock object for the purpose of Test Driven Development and Behavior Driven development. Unlike creating actual object, Mockito allows creation of fake object (external dependencies) which allows it to give consistent results to a given invocation. 2.       Question 2. Why Do We Need Mockito? What Are The Advantages? Answer : Mockito differentiates itself from the other testing framework by removing the expectation beforehand. So, by doing this, it reduces the coupling. Most of the testing framework works on the "expect-run-verify". Mockito allows it to make it "run-verify" framework. Mockito also provides annotation which allows to reduce the boilerplate code. 3.       Question 3. Can You Explain A Mockito Framework? Answer : In Mockito, you always check a particular class. The dependency in that class is injected using m...

JAVA Expert Interview Questions Answers 2017

Java Basics ::  Interview Questions and Answers Home  »  Interview Questions  »  Technical Interview  »  Java Basics  » Interview Questions 1.     What is the difference between a constructor and a method? A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. 2.     What is the purpose of garbage collection in Java, and when is it used? The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used. 3.  ...

Java Example Program to Convert List to Set

import java.util.ArrayList; import java.util.HashSet; import java.util.Set; public class ListToSet {  /**   * @author Amarjit Kumar   * @category interview questions   *   * Description: Convert List to set in java with example program   *   */  public static void main(String[] args) {   ArrayList<String> arrList= new ArrayList<>();   arrList.add("Java");   arrList.add("Java");   arrList.add("List to String");   arrList.add("Example Program");   Set<String> strSet = new HashSet<String>(arrList);   System.out.println(strSet);   System.out.println(arrList);  } } /*  * Java program to convert list to set. Convert ArrayList of string to HashSet  * in java example program How to convert List to Set in java Set<String> strSet  * = new HashSet<String>(arrList); HashSet having a constructor which will take  * list as an ar...