I have the following global variable of String type.
static public String rev="hello"
I can read it without any issue from an object of another class. Is there anyway I can update it with another string from an object of another class? I know Java string is immutable. I tried with the following way using StringBuilder:
static public StringBuilder rev=new StringBuilder("hello");
static public void setRev(StringBuilder s)
{
rev=rev.delete(0,rev.length());
rev.append(s);
}
From another class:
MainActivity.setRev(stringBuilderVar);
But it did not work.
rev(saysomeOtherVar = MainActivity.rev), reassigningrevwon't change anything where you previously copied the reference. Without more information, it's hard to tell what the right way to do things is. But I'm pretty sure thatStringBuilderis the wrong solution.statichas nothing to do with immutability.staticmeans that the variable belongs to the whole class and not to instances of the class. And you can access non-static variables from another class by creating objects of the class, which is the normal way to do things. I strongly suggest you work through a tutorial on Object-Oriented Programming concepts in Java, such as docs.oracle.com/javase/tutorial/java/concepts.