jadrijan
jadrijan

Reputation: 1446

java - public static String get "reference"

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.

    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

    }

Upvotes: 0

Views: 17277

Answers (7)

cdeszaq
cdeszaq

Reputation: 31300

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

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

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 String is changing the String the variable is referencing.

Upvotes: 3

Tudor
Tudor

Reputation: 62459

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

class StringWrapper {
    public String value;
}

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

Upvotes: 2

kaliatech
kaliatech

Reputation: 17887

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.

Upvotes: 4

twain249
twain249

Reputation: 5706

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 str in 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.

Upvotes: 2

Java42
Java42

Reputation: 7706

Change:

this.str = str;

To:

Globals.str = str;

Upvotes: 1

blank
blank

Reputation: 18180

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.

Upvotes: 2

SLaks
SLaks

Reputation: 887887

Strings are immutable.

You're passing a reference to an immutable String instance, not to the mutable str variable.

Upvotes: 1

Related Questions