java - 公共静态字符串获取“引用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9670125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:56:00  来源:igfitidea点击:

java - public static String get "reference"

javastringstaticpublic

提问by jadrijan

I don't understand something regarding the public static Strings. I have a couple of variables that need to be accessed globaly (I know that that is not the true OO approach). If I pass the "reference" of public static String str from Globals class, any change made to the value in SomeClass will not update that variable.

我不明白有关公共静态字符串的内容。我有几个需要全局访问的变量(我知道这不是真正的 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

    }

回答by cdeszaq

It's because you are not calling the "global" strvariable, but instead are calling the class-local strvariable.

这是因为您没有调用“全局”str变量,而是调用类局部str变量。

Without additional information about what strvariable you are wanting to change, Java will use the most tightly-scoped variable with the given name. Just like you did with this.strin the constructor to indicate you wanted the private instance variable of the SomeClassclass, you would need to do Globals.strto indicate you wanted the public static strvariable that you are using as a global.

如果没有关于str您要更改的变量的附加信息,Java 将使用具有给定名称的最紧密范围的变量。就像您this.str在构造函数中所做的以表明您想要类的私有实例变量一样SomeClass,您需要这样做Globals.str以表明您想要public static str作为全局变量使用的变量。

Also, as others have pointed out, Strings are immutable in Java, so what you are really doing when you assign to any variable of type Stringis changing the Stringthe variable is referencing.

此外,正如其他人指出的那样,Strings 在 Java 中是不可变的,因此当您分配给任何类型的变量时,您真正在做的String是更改String引用的变量。

回答by SLaks

Strings are immutable.

字符串是不可变的。

You're passing a reference to an immutable Stringinstance, not to the mutable strvariable.

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

回答by blank

The str class variable is declared statically for the Globals class not for every class in an application. The str in Someclass has no relation to the str in Globals - they just happen to have the same identifier.

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

回答by Tudor

This is because in Java parameters are passed by value, not reference. Thus, assigning a new value to the Stringobject is not seen outside the method. You can use a wrapper to achieve this:

这是因为在 Java 中参数是按值传递的,而不是引用。因此,String在方法之外看不到为对象分配新值。您可以使用包装器来实现这一点:

class StringWrapper {
    public String value;
}

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

回答by Java42

Change:

改变:

this.str = str;

To:

到:

Globals.str = str;

回答by twain249

public class Globals{

public static String str;

}

public class SomeClass{

private String str;

}

Those 2 strings are not the same string (you should change one of their names). To access the strin Globals you'll have to use Globals.str. Also Strings are immutable so you don't actually change the string but create a new string and assign the value to the new one.

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

回答by kaliatech

Your scope is ambiguous. Did you mean this:

你的范围不明确。你是不是这个意思:

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;
}

or this:

或这个:

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;
}

Hopefully that helps.

希望这有帮助。