I would like to know the reason that whenever inside an anonymous class I try to change the value of an "external" variable it does not, changes remain the same. Would there be something similar that could do that would give the same result?
I used Thread as an example, but in any anonymous class this happens. (Including Android)
public class Teste {
public boolean umIgualUm = false;
public Teste() {
fazerAlgo();
}
public void fazerAlgo() {
new Thread() {
public void run() {
if (1 == 1) {
System.out.println("Entra aqui");
umIgualUm = true;
}
}
}.start();
System.out.println(umIgualUm); //Exibe false
}
}
Android example
public class UsuarioDAO {
public boolean metodoDeuCerto = false;
public UsuarioDAO(){}
public boolean cadastrarUsuario(Usuario usuario) {
ParseUser parseUser = new ParseUser();
parseUser.setUsername(usuario.getNome());
parseUser.setEmail(usuario.getEmail());
parseUser.setPassword(usuario.getSenha());
parseUser.signUpInBackground(new SignUpCallback() {
@Override
public void done(ParseException e) {
if(e == null) { //Quer dizer que deu certo
metodoDeuCerto = true;
}else{
e.printStackTrace();
metodoDeuCerto = false;
}
}
});
return metodoDeuCerto;
}
}