package raja.JavaPages4All; import java.util.Date; /** * @author Raja(JavaPages4All) */ public class TestPojo { private String blogName; private Date date; public TestPojo(String blogName,Date date) { this.blogName=blogName; this.date = date; } public String getBlogName() { return this.blogName; } public Date getDate() { return this.date; } } |
package raja.JavaPages4All; import java.util.Date; public class ImmutableTest { /** * @author Raja(JavaPages4All) */ public static void main(String[] args) { /* * String Class is Immutable. * Date Class is Mutable. */ String str="JavaPages4All"; Date myDate = new Date(); TestPojo pojo =new TestPojo(str,myDate); /** * Getting Values from TestPojo Reference Object(When First Time) */ System.out.println(pojo.getBlogName()); System.out.println( pojo.getDate() ); /** * String str Object value changing so it is Creating Newly(Immutable). * Date myDate Object value changing so it is Updated the * Previous Object myDate Value(Mutable) */ str="Raja"; myDate.setMonth( myDate.getMonth() + 1 ); /** * Getting Values from TestPojo Reference Object * (When Second Time object Values Changed) */ System.out.println(pojo.getBlogName()+" "+str); System.out.println( pojo.getDate() ); } } |
No comments:
Post a Comment