Monday, 13 April 2015

How to create Immutable class? (String Session-7)

How to create Immutable class?

There are many immutable classes like String, Boolean, Byte, Short, Integer, Long, Float, Double etc. In short, all the wrapper classes and String class is immutable. We can also create immutable class by creating final class that have final data members as the example given below:

Example to create Immutable class

In this example, we have created a final class named Employee. It have one final datamember, a parameterized constructor and getter method.
1.    public final class Employee{  
2.    final String pancardNumber;  
3.      
4.    public Employee(String pancardNumber){  
5.    this.pancardNumber=pancardNumber;  
6.    }  
7.      
8.    public String getPancardNumber(){  
9.    return pancardNumber;  
10. }  
11.   
12. }  

The above class is immutable because:
·         The instance variable of the class is final i.e. we cannot change the value of it after creating an object.
·         The class is final so we cannot create the subclass.
·         There is no setter methods i.e. we have no option to change the value of the instance variable.
These points makes this class as immutable.

Java toString() method

If you want to represent any object as a string, toString() method comes into existence.
The toString() method returns the string representation of the object.
If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.

Advantage of Java toString() method

By overriding the toString() method of the Object class, we can return values of the object, so we don't need to write much code.

Understanding problem without toString() method

Let's see the simple code that prints reference.
1.    class Student{  
2.     int rollno;  
3.     String name;  
4.     String city;  
5.      
6.     Student(int rollno, String name, String city){  
7.     this.rollno=rollno;  
8.     this.name=name;  
9.     this.city=city;  
10.  }  
11.   
12.  public static void main(String args[]){  
13.    Student s1=new Student(101,"Raj","lucknow");  
14.    Student s2=new Student(102,"Vijay","ghaziabad");  
15.      
16.    System.out.println(s1);//compiler writes here s1.toString()  
17.    System.out.println(s2);//compiler writes here s2.toString()  
18.  }  
19. }  
Output:Student@1fee6fc
       Student@1eed786
As you can see in the above example, printing s1 and s2 prints the hashcode values of the objects but I want to print the values of these objects. Since java compiler internally calls toString() method, overriding this method will return the specified values. Let's understand it with the example given below:

Example of Java toString() method

Now let's see the real example of toString() method.
1.    class Student{  
2.     int rollno;  
3.     String name;  
4.     String city;  
5.      
6.     Student(int rollno, String name, String city){  
7.     this.rollno=rollno;  
8.     this.name=name;  
9.     this.city=city;  
10.  }  
11.    
12.  public String toString(){//overriding the toString() method  
13.   return rollno+" "+name+" "+city;  
14.  }  
15.  public static void main(String args[]){  
16.    Student s1=new Student(101,"Raj","lucknow");  
17.    Student s2=new Student(102,"Vijay","ghaziabad");  
18.      
19.    System.out.println(s1);//compiler writes here s1.toString()  
20.    System.out.println(s2);//compiler writes here s2.toString()  
21.  }  
22. }  
Output:101 Raj lucknow

       102 Vijay ghaziabad

No comments:

Post a Comment

Access attributes in component

NOTE: To access an attribute in a  component , use expressions as  {! v.<Attribute Name>} . ----------------------------------------...