Results 1 to 7 of 7
- 12-18-2011, 09:15 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
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); } }I want to make my Student class immutable.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()); } }
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?
- 12-18-2011, 09:26 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,588
- Blog Entries
- 7
- Rep Power
- 17
Re: How can I make this class immutable?
Return a copy of the Date object?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-18-2011, 09:26 PM #3
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: How can I make this class immutable?
Sorry ?
- 12-18-2011, 09:30 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,588
- Blog Entries
- 7
- Rep Power
- 17
Re: How can I make this class immutable?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-18-2011, 09:37 PM #5
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: How can I make this class immutable?
Thanks.
If it is not too much trouble can you make an example with codes?
-
Re: How can I make this class immutable?
Last edited by Fubarable; 12-18-2011 at 10:02 PM.
- 12-18-2011, 10:08 PM #7
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Similar Threads
-
Make JVM use My Own Class Loader
By rajyshubeita@gmail.com in forum Advanced JavaReplies: 1Last Post: 11-18-2011, 09:41 PM -
how to write user defined class as Immutable?
By srinivasmallabathula in forum New To JavaReplies: 3Last Post: 07-04-2011, 11:50 PM -
Can't make a java .class to a .jar
By TheDarkMagician in forum New To JavaReplies: 4Last Post: 02-19-2011, 11:45 PM -
Deleted Class template code. Now I cant make class.
By AcousticBruce in forum IntelliJ IDEAReplies: 0Last Post: 01-11-2011, 10:52 PM -
What is an Immutable Class
By maheshkanda in forum New To JavaReplies: 3Last Post: 02-06-2009, 08:12 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks