File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes immutable object ???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "immutable object ????" Watch "immutable object ????" New topic

immutable object ????

Mona Gadkari
Ranch Hand

Joined: Jul 28, 2003
Posts: 64
see this code:::
private int[] val;
private int hash;
public int hashCode() {
int h = hash;
if (h == 0) {
int len = val.length;
for (int i = 0; i < len; i++) {
h = 31*h + val[i];
hash = h;
return h;
// The equals method has been omitted for clarity.
A (int[] val) {this.val = (int[])val.clone();}
public static void main (String[] args) {
A a = new A(new int[]{1,2,3});

o/p::: 1026 i too got this which is correct ans indeed.But my que is why is this obj immutable.
As provided in the explaination:::
The hashCode is calculated using all three array elements. Since the object is immutable, the hashcode is only calculated the first time the hashCode method is invoked
pls explain.

Jessica Sant

Joined: Oct 17, 2001
Posts: 4313

Welcome to the JavaRanch! Couple things for ya....
1st thing:
Please adjust your displayed name to meet the JavaRanch Naming Policy. Basically it should be both a first AND last name (seperated by a space of course). You can change it here. Thanks!!
2nd thing:
Also, when you're posting code... try surrounding them in [CODE] tags. It helps to preserve the whitespace and makes it easier for us to help you out.
Thanks! And again, welcome to the JavaRanch.

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Object of type A seems to be immutable since there's no way to change the int array object passed to the constructor after the object has been constructed. All the instance variables used for calculation hashCode() are private as well.

If your class is immutable (the instance data cannot be modified once initialized), you can base the hash code on the data field values. You should even calculate the hashCode() just once for an instance (after all, no data is going to change after the object has been instantiated - the class is immutable) and store it in a private instance variable. Next time onwards, the hashCode() method can just return the private variable's value, making the implementation very fast.

Read this thread about immutable objects and their equals() and hashCode() implications:
Implementing equals() and hashCode()
[ Jess modified the CODE tags to QUOTE tags so that it all wouldn't be on one giant line ]
[ November 18, 2003: Message edited by: Jessica Sant ]
mrudul joshi
Ranch Hand

Joined: Nov 12, 2003
Posts: 54
I didnt understand why the output came like that.I tried to run the program and got the same output, but how does program control flow?Why is the clone object created ?
will you please explain/Thank you very much
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
clone makes a copy of the argument, now the object passed to the constructor can be modified (by code out of the constructor) without interferring with the copy passed. Thus the hashcode will be always the same, and it needs be calculated only once.

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link:
subject: immutable object ????
It's not a secret anymore!