2

How can I make sure that the value of String variable doesnot change after being assigned once? Assignment is not at the time of declaration.

More clarity :-

String x = y; (y is another string) Even if y changes x should not change. How to make sure this?

What I have tried :-

Created a custom class:-

public class MyDs {
    private final String unitName;

    public MyDs(String iUnitName){
        unitName   =   iUnitName;

    }

    public String getUnitName(){
        return unitName;
    }
}

in the main method :-

String iName = "xyz";

MyDs MyDsObj  = new MyDs(iName);

But even after this, the value changes when the variable changes.

How can I solve this issue?

Sreekanth Karumanaghat
  • 3,383
  • 6
  • 44
  • 72

4 Answers4

0

Change your MyDs class to a singleton class

Making this a singleton class ensures that the final String unitName is updated only once and then it will cannot be altered again.

public class MyDs {
    private final String unitName;
    private static MyDs myDs;

public static MyDs getMyDsObject(String iUnitName) {
    if (myDs == null) {
        myDs = new MyDs(iUnitName);
    }
    return myDs;
}

private MyDs(String iUnitName) {
    unitName = iUnitName;

}

public String getUnitName() {
    return unitName;
}

}

Here the values "xyz" is stored in unitName and doesnot get updated again when you change to "zxy".

MyDs MyDsObj = MyDs.getMyDsObject("xyz");
Log.i("value", "" + MyDsObj.getUnitName());

MyDs MyDsObj1 = MyDs.getMyDsObject("zxy");
Log.i("value",""+MyDsObj.getUnitName());
Sharath kumar
  • 4,064
  • 1
  • 14
  • 20
0

Your class be should be design as mentioned in below code

public class TestingClas {

private String name;

public void setName(String name) {
    if (this.name == null && name != null)
        this.name = name;
}

public String getName() {
    return name;
}

}

Now use below code for testing purpose

   TestingClas testingClas = new TestingClas();
    testingClas.setName("Abdul Waheed");
    testingClas.setName("You cannot change me any more now");

    String updatedString = testingClas.getName();

updatedString variable will be having old value

Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
0

as far as I understand ,you should design your class in a way that your variable should be final . with this approach you set it in constructor and then nothing can make it changes. even the referance it is holding is changed the value remains the same I mean a new object is created in heap and value of your final variable is kept same. below is a kind of design which makes the variable x set once and never be able to changed afterwards. Of course this is in instance scope, for class scope you can make your class singelton etc.

public class Test {

    private final String x;
    private String y;


    public Test(String x){
        this.x=x;
    }

    public String getY() {
        return y;
    }

    public void setY(String y) {
        this.y = y;
    }

    public String getX() {
        return x;

    }

}
0

Well, you question is not really clear (and the comment section is really chaty...), but if you want to only be able to set a value once but not during the initialisation, setters are not a bad choice. Just add a constraint.

public class MyClass{

    private static final String DEFAULT_VALUE = new String("");

    private String value = DEFAULT_VALUE;

    public final void setValue(String value){
        if(this.value != DEFAULT_VALUE) //use the reference on purpose
            throw new IllegalArgumentException("This value can't be changed anymore");

        this.value = value;
    }

    // Don't return `DEFAULT_VALUE` to prevent someone to gain access to that instance
    public final String getValue(){            
        return this.value == DEFAULT_VALUE ? null : this.value;
    }
}

This will be done at runtime, but this would do the trick.

Now, this is an immutable instance, with some mutable instance you might want to do a copy of it to be sure it can't be modifier using the original reference.

AxelH
  • 14,325
  • 2
  • 25
  • 55