I have some question about anonymous inner-class. I just realize that in my anonymous inner-class can refer to two variable with the same name to outter class and in ineer-class it self, How do I know that what it refer to at that moment.
This is my code.
public class RealmUpdate {
private Realm realm;
public void upsertUserProfile( final String email, final String fname, final String lname,
final String gender, final String birthdate, final String tel){
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(@NonNull Realm realm){
try{
realm = Realm.getDefaultInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String[] arr_date = birthdate.split("/");
String real_date = arr_date[2] + "-" + arr_date[1] + "-" + arr_date[0];
Log.d( getClass().getSimpleName(), real_date);
Date date = format.parse(real_date);
UserProfile data = realm.where(UserProfile.class)
.equalTo("Email", email)
.findFirst();
if( data == null){
Log.d(RealmUpdate.class.getSimpleName(), "No account in DB.");
UserProfile userProfile = realm.createObject(UserProfile.class, email);
Log.d( getClass().getSimpleName(), "Value add: Start!?");
userProfile.setFName(fname);
Log.d( getClass().getSimpleName(), "Value add: fName!?");
userProfile.setLName(lname);
Log.d( getClass().getSimpleName(), "Value add: lName!?");
userProfile.setGender(gender);
userProfile.setBirthdate(date);
userProfile.setTel(tel);
Log.d( getClass().getSimpleName(), "Value add!?");
realm.close();
return;
}
Log.d(RealmUpdate.class.getSimpleName(), "Found account in DB");
data.setEmail(email);
data.setFName(fname);
data.setLName(lname);
data.setGender(gender);
data.setBirthdate(date);
data.setTel(tel);
realm.close();
} catch(ParseException exception) {
realm.close();
exception.printStackTrace();
Log.e( getClass().getSimpleName(), "Catch Error!!!");
}
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
Log.d( getClass().getSimpleName(), "Add profile success");
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
realm.close();
Log.d( getClass().getSimpleName(), "Add profile not success");
}
});
realm.close();
}
From my code, I have 2 Realm object that name "realm" the first is declare in main class and others one is declare in anonymous inner-class. When I try to change the name Realm object of main class to
private Realm realm_test;
it only show the error at
realm.executeTransactionAsync(new Realm.Transaction() {
, the code in anonymous inner-class still fine. But when I change the name of variable in anonymous inner class like this,
public void execute(@NonNull Realm realm_test_inner){
the code in anonymous inner-class got no error too.
Now, I'm very confuse with these because it cause the error when I insert data to database(can fixed it now), but I'm still not sure what does it really happen?
This feature is named shadowing. You can read about it here:
https://docs.oracle.com/javase/tutorial/java/javaOO/nested.htm
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.