Example:Immutable Class

Immutable class is thread safe
Imuutable class objects cannot be modified
Examples :
  • Integer
  • String
package com.demo.immutable;
//make ur class final
public final class MyImmutable {
 //make ur instance variables final
    final String s="Anjana";
    private String s2=null;
//access the private via constructors--to modify
    public MyImmutable(String s2) {
        super();
        this.s2 = s2;
    }
    public static void main(String args[]){
        MyImmutable  myimmutable=new MyImmutable("dff");
    }
}
 

Comments

Popular posts from this blog

defining functions clojure

How to retain the space of the td after hidding it in Jquery

Integrating Struts2 with Spring Security using Custom Login Form