Friday, July 22, 2016

String is Immutable and property hash is not final




How is that possible? We have read all over the internet that all properties must be final to make a class immutable.

Well it is possible but with some rules/restrictions and that is access to mutable properties/fields must provide same result every time we access it.

In String class hashcode actually calculated on the final array of characters which is not going to change if String has constructed. Therefore immutable class can contain mutable fields/properties but it has to make sure that access to field/property will produce the same result every time it is accessed.

Can we create a immutable class without marking it as final?

Most of java developers (may be 90%) will answer this question with a big "NOooo" and its absolutely fine to answer with a big "NOooo" because its written everywhere on internet that for a class to be immutable it must be final but surprisingly we can create a immutable class without declaring it as final.

So the answer of the above question is "YES" we can create a immutable class without making it as final but how???

Let me tell you one of such example from java classes itself  "BigInteger" class is immutable but its not final



As marked in red BigInteger is not a final class.

Let's test BigInteger's immutability now

Since BigInteger is immutable therefore the output of the above program will be 0.

Actually Immutability is a concept according to which ones the object created then it can not be modified.

Let's think from JVM point of view, from JVM point of view all threads must share the same copy of the object and it is fully constructed before any thread accesses it and the state of the object doesn't change after its construction.

Immutability means there is no way yo change the state of the object once it is created and this is achieved by three thumb rules which makes the compiler to recognize that class is immutable and they are as follows :-

  • all non private fields should be final
  • make sure there is no method in the class that can change the fields of the object either directly or indirectly
  • any object reference defined in the class can't be modified outside from the class

Therefore for creating immutable class it is not mandatory to mark the class as final.

Static methods are redefined not overidden...

What will be out put of the following program?
--------------------------------------------------------------
class StaticSuper {
 public static void m1() {
  System.out.println("Super m1");
 }
 public void m2() {
  System.out.println("Super m2");
 }
 public void m3() {
  m1();
  m2();
 }
}
class StaticSub extends StaticSuper {
 public static void m1() {
  System.out.println("Sub m1");
 }
 public void m2() {
  System.out.println("Sub m2");
 }
}
public class Test {
 public static void main(String args[]) {
  StaticSuper ss = new StaticSub();
  ss.m3();
 }
}
--------------------------------------------------------------
Output of the following program will be :
Super m1
Sub m2

The output of the above program proves that Static methods are not overridden they are redefined.
If you try to override the static method then it will be treated as a non overridden method of the sub class. Static methods will be called based on the type of the reference not the object created.