0
I would like to know why whenever within an anonymous class I try to change the value of an "external" variable it does not, change remains the same. There would be something similar that I could do that would give the same result?
I used the Thread as an example, but in any anonymous class it happens. (Including on 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;
}
}
I actually believe it’s because of the timing of things within the anonymous class happening. As in the example posted to startar the thread System.out.println runs right away and did not have time to finish the run process()
– André Ozawa
Are you sure this happens in case it is not "in a Thread"? In the use of "a Thread" it happens because when a thread
System.out.println(umIgualUm);
the line is executedumIgualUm = true;
(may) not (have been)executed.– ramaral
What other anonymous class could I test with?
– Hamon
On Android you can test for example on
OnClickListener
, assigned to a button.– ramaral
I put the example of Android, what is really happening.
– Hamon
In this example the same happens. The method
done()
is called, by the implementation ofsignUpInBackground()
, after the linereturn metodoDeuCerto;
have been executed.– ramaral
And how to do to give a return to the user whether it worked or not the registration? For since I can not do so, nor give the Return inside the method of the anonymous class (of course). How to do?
– Hamon
It depends on how you want to inform the user. However, whatever this way, it has to be done within the method
done()
or in a method called by him.– ramaral
I needed to inform this user, return either to Exception or a Boolean, but I needed to return because it is not this class that will take care to warn the user with some message.
– Hamon
In this case pass Signupcallback to the method
cadastrarUsuario()
or, perhaps better, create a new interface/class to receive the result.– ramaral
Would you have some way to wait for Signupcallback to finish so you can return?
– Hamon
If that’s what you really want, you can, see the documentation
– ramaral
Right, but I still don’t understand how to wait for my "main" thread to make this one wait. Sorry, I’m not as experienced with java.
– Hamon
That’s why I won’t tell you how. I don’t want to contribute to you starting to learn the wrong way :)
– ramaral
Well I did some research and I didn’t get anything so relevant. I wanted the idea to take the Thread of the User class and ask to wait or something. I have the idea of using the Join() method but I don’t know how exactly. Honestly, can you give me a hand, I’d like to do this soon, I’m getting distressed.
– Hamon