Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Gail Knight   on Jun 14 In Java Category.

  
Question Answered By: Mona Wagner   on Jun 14

Sure... Just don't include any methods that change the state of the object
and make your class final. There is no real magic behind strings - except
for the overloaded + operator (grumble grumble... too bad Sun apparently
thought that we were to stupid to do our own operator overloading... grumble
grumble....). It just doesn't include any methods to change it's internal
state...

eg:

final class C1
{
private int value;

public C1(int v)
{
this.value = v;
}


/* Rather than doing:

public C1 add(int val)
{
this.value += val;
}

do...
*/


public C1 add(int val)
{
return new C1(this.value + val);
}
}

Share: 

 

This Question has 8 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on String Object Or get search suggestion and latest updates.


Tagged: