mercredi 8 mars 2017

Random number changes each time I access the variable

I am trying to generate and then save a random number.

However every time I access the variable that contains the random String, it get's re-created i.e. the variable is then set to a new random String.

I'm building some 'forgotten password' functionality.

The main class looks like this:

public class PasswordRecoverer{

@Id
public ObjectId id;
public String email;
public String key;
public Long expires;

public PasswordRecoverer create(String email){

    this.email = email;
    this.key = getKey();
    Calendar cal =  Calendar.getInstance();
    this.expires = cal.getTimeInMillis() + 3600000; // expires in 1hr

    return this;
}

// more code here ...

}

I am accessing (and printing) the key variable in my controller class, and I can see it changes each time.

Btw: p.rint() that I use below is just a class.method() shorthand for System.out.println()

PasswordRecoverer pr = new PasswordRecoverer();
    p.rint(1, pr.key);
    if(new PasswordRecoverer().read(email)==null){
        p.rint(2, pr.key);
        pr = new PasswordRecoverer().create(email);
        p.rint(3,pr.key);
    }
    else{
        p.rint(4,pr.key);
        pr = new PasswordRecoverer().update(email);
        p.rint(5,pr.key);
    }
    p.rint(6, pr.key);
    pr.save();
    p.rint(7,pr.key);
    p.rint(pr.key);
    p.rint(8,pr.key);

Everytime I do p.rint(x,pr.key) ... pr.key has a different value. I would like it to be the same each time.

Aucun commentaire:

Enregistrer un commentaire