0

I have this setHash function that is asynchronous:

public void setHash(Context context, String hash) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("has", hash);
    editor.apply();
}

I use it with the following code:

private String mHash = "s4mp1eh45h";

void myFunction() {
    setHash(this, mHash);
    mHash = null;
}

I know that java is 'pass-by-value' per this post. Now my question is, is there a chance that the hash that gets saved in my setHash function is null?

user1506104
  • 6,554
  • 4
  • 71
  • 89
  • Yes, since Java inherits pass-by-value concept from C. It's said that it will be stored in to preference no matter on which thread you change variable's value. – Jeel Vankhede Oct 07 '22 at 07:31
  • What is the "editor" variable exactly as a class type? Relating to editor.putString("has", hash) what types does SharedPreferences.Editor extend from? And is it something from java.util? – Samuel Marchant Oct 07 '22 at 07:46
  • Also, is your myFunction() method in the same class with setHash or in scope subclass(ultimately the same)? – Samuel Marchant Oct 07 '22 at 07:54
  • Just a point with java.util.* , always in full ANYWHERE e.g. java.util.HashMap hsmp = new java.util.HashMap(); NEVER implicit, also try this but is possibly not the problem but can be for keys, .putString(((Object)"key1") , elementvalue); quite absurd when it happens with java.util.HashMap() – Samuel Marchant Oct 07 '22 at 08:11
  • Technically this line mHash = null; "is an error" because it cannot set mHash null the method myFunction is only default access not private access! Only a method of the same access level as the variable can set the variable! During and from the constructor is the only other can set them. – Samuel Marchant Oct 07 '22 at 08:48
  • @SamuelMarchant A1: editor is part of android library. A2: same class as setHash. A3: I dont use hashmap here. I was just given a string with hash inside. I have no idea how the hash was generated. A4: mHash=null is valid. My code runs with these codes. – user1506104 Oct 07 '22 at 09:20
  • SharedPreferences is from the context so no control over what storage system is called by putString . A4 would given trouble as I see it but, Can you test that by using what I said , raise the method to private and then the key with Object? – Samuel Marchant Oct 07 '22 at 10:29
  • Good point in this, the other night I got my usual problem, compile, run, a 400 line class but will not get my "predicted" basic test output for it. So why. After carefully trying too many times and optimising code in the sequence of methods and lines to best practice I got something alike "negative array index illegal assignment" . The subtraction on the array indexes in use were back to front. The initial error only gave the method on it at the original start and the program ran giving extremely minor error info at runtime but output through the sequences to end of processing – Samuel Marchant Oct 07 '22 at 11:20
  • The ultimate in that is getting no error message inclusive at runtime but it does not get the correct result. – Samuel Marchant Oct 07 '22 at 11:26
  • If the class itself is a Context WHY not say "this" inside method setHash and WHY pass it on by argument to any method? Just use "this" once where you get the editor. setHash(String hash). private void myFunction(){setHash(mHash); – Samuel Marchant Oct 07 '22 at 11:46

0 Answers0