Results 1 to 7 of 7
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default How can I make this class immutable?

    Java Code:
    package myPackage;
    
    import java.util.*;
    
    public final class Student {
    
    	private int						id;
    	private String					name;
    	private final java.util.Date	dateCreated;
    
    	public Student(int ssn, String newName) {
    		id = ssn;
    		name = newName;
    		dateCreated = new java.util.Date();
    	}
    
    	// end constructor
    
    	public int getId() {
    		return id;
    	}
    
    	// end getId
    
    	public String getName() {
    		return name;
    	}
    
    	// end getName
    
    	public Date getDateCreated() {
    		return dateCreated;
    	}
    	// end getDateCreated
    
    }
    Java Code:
    package myPackage;
    
    public class PrinterClass {
    	
    	public void printer(String text)
    	{
    		System.out.println(text);
    	}
    	
    	public void printer(int text)
    	{
    		System.out.println(text);
    	}
    	
    	public void printer(Object o)
    	{
    		System.out.println(o);
    	}
    
    }
    Java Code:
    package myPackage;
    
    import java.util.Date;
    
    public class Test {
    
    	public static void main(String[] args) {
    		
    		Student student = new Student(123,"John");
    		
    		PrinterClass printer = new PrinterClass();
    		
    		printer.printer(student.getId());
    		printer.printer(student.getName());
    		printer.printer(student.getDateCreated());
    		
    		Date myDate = student.getDateCreated();
    		myDate.setMonth(1);
    		printer.printer(student.getDateCreated());
    		
    		
    	}
    }
    I want to make my Student class immutable.

    But as you can see I am able to play with the dateCreated field by:

    Date myDate = student.getDateCreated();
    myDate.setMonth(1);

    Is there anyway to protect my dateCreated field?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: How can I make this class immutable?

    Return a copy of the Date object?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: How can I make this class immutable?

    Sorry ?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: How can I make this class immutable?

    Quote Originally Posted by fatabass View Post
    Sorry ?
    Instead of returning your dateCreated object, return a copy of it; so the caller/receiver can only modify that copy while the original object remains unaltered.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: How can I make this class immutable?

    Thanks.

    If it is not too much trouble can you make an example with codes?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How can I make this class immutable?

    Quote Originally Posted by fatabass View Post
    If it is not too much trouble can you make an example with codes?
    Um,...it's one line of code for gosh's sake. Just try it. Calling clone() would do the trick.
    Last edited by Fubarable; 12-18-2011 at 10:02 PM.

  7. #7
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

Similar Threads

  1. Make JVM use My Own Class Loader
    By rajyshubeita@gmail.com in forum Advanced Java
    Replies: 1
    Last Post: 11-18-2011, 09:41 PM
  2. how to write user defined class as Immutable?
    By srinivasmallabathula in forum New To Java
    Replies: 3
    Last Post: 07-04-2011, 11:50 PM
  3. Can't make a java .class to a .jar
    By TheDarkMagician in forum New To Java
    Replies: 4
    Last Post: 02-19-2011, 11:45 PM
  4. Deleted Class template code. Now I cant make class.
    By AcousticBruce in forum IntelliJ IDEA
    Replies: 0
    Last Post: 01-11-2011, 10:52 PM
  5. What is an Immutable Class
    By maheshkanda in forum New To Java
    Replies: 3
    Last Post: 02-06-2009, 08:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •