java—公共静态字符串get";参考资料;

java—公共静态字符串get";参考资料;,java,string,static,public,Java,String,Static,Public,我不明白关于公共静态字符串的一些事情。我有几个 需要全局访问的变量(我知道这不是真正的OO方法)。 如果我从Globals类传递公共静态字符串str的“引用”,那么对SomeClass中的值所做的任何更改都不会更新 这个变量 public class Globals{ public static String str; } public class SomeClass{ private String str; public void se

我不明白关于公共静态字符串的一些事情。我有几个 需要全局访问的变量(我知道这不是真正的OO方法)。 如果我从Globals类传递公共静态字符串str的“引用”,那么对SomeClass中的值所做的任何更改都不会更新 这个变量

    public class Globals{

    public static String str;

    }

    public class SomeClass{

    private String str;

    public void setStr(String str){
        this.str = str; 
        //If I change the value of str in this SomeClass, the value does not get
        //updated for the public static String str in Globals class
    }

//Here assign new value for str

    }

这是因为您没有调用“global”
str
变量,而是调用类local
str
变量

    public class Globals{

    public static String str;

    }

    public class SomeClass{

    private String str;

    public void setStr(String str){
        this.str = str; 
        //If I change the value of str in this SomeClass, the value does not get
        //updated for the public static String str in Globals class
    }

//Here assign new value for str

    }
如果没有关于您想要更改的
str
变量的附加信息,Java将使用范围最窄的给定名称变量。就像您在构造函数中使用
this.str
来表示您需要
SomeClass
类的私有实例变量一样,您需要执行
Globals.str
来表示您需要将
公共静态str
变量用作全局变量

另外,正如其他人所指出的,
String
s在Java中是不可变的,因此当您将
String
类型的变量赋值给任何变量时,您真正要做的是更改变量引用的
String

字符串是不可变的


您正在传递对不可变的
String
实例的引用,而不是对可变的
str
变量的引用。

str class变量是为Globals类静态声明的,而不是为应用程序中的每个类声明的。Someclass中的str与Globals中的str没有关系-它们恰好具有相同的标识符。

这是因为在Java中,参数是通过值传递的,而不是通过引用传递的。因此,在方法外部看不到为
字符串
对象指定新值。您可以使用包装器来实现这一点:

    public class Globals{

    public static String str;

    }

    public class SomeClass{

    private String str;

    public void setStr(String str){
        this.str = str; 
        //If I change the value of str in this SomeClass, the value does not get
        //updated for the public static String str in Globals class
    }

//Here assign new value for str

    }
class StringWrapper {
    public String value;
}

public void setString(StringWrapper wrapper) {
    wrapper.value = "some value"; // the String inside wrapper is changed
}
更改:

this.str = str;
致:


这两个字符串不是同一个字符串(您应该更改其中一个名称)。要访问Globals中的
str
,必须使用
Globals.str
。此外,字符串是不可变的,因此您实际上不会更改字符串,而是创建一个新字符串并将值分配给新字符串。

您的范围不明确。你是说:

public void setStr(String str){
    this.str = str; 
    //If I change the value of str in this SomeClass, the value does not get
    //updated for the public static String str in Globals class
   Globals.str = this.str;
}
或者这个:

public void setStr(String str){
    this.str = str; 
    //If I change the value of str in this SomeClass, the value does not get
    //updated for the public static String str in Globals class
    this.str = Globals.str;
}

希望这能有所帮助

为什么您会认为这两个
str
字段(位于
Globals
中的字段和位于
SomeClass
中的字段)是相关的?您能给我们您的
setStr()
调用吗?这只适用于通过引用传递对象的基本体。@twain249,不,对象根本不传递。(虽然对对象的引用是通过值传递的,但是那些也是通过值传递的。)对
字符串的引用仍然是通过值传递的。将不同的引用分配给
字符串
参数将不会在外部看到。从技术上讲,范围并不含糊。Java作用域规则要求,在没有像
this
ClassName
classInstance
这样的额外cope指示符的情况下,使用具有给定名称的最小作用域变量。也就是说,你的例子确实使区别更加明确。同意。我的意思是这个问题模棱两可,因为我不确定OP想要使用哪个范围。@BedwyrHumphreys-很好。我最初没有阅读代码示例。为了反映这一点,我修改了我的回答。谢谢。字符串可能确实是不可变的,但在这种情况下它与此无关。@BedwyrHumphreys:他可能认为他在改变现有实例。