lundi 15 août 2016

How to modify enclosing class reference?

I have enclosing and nested classes like this:

public class Promotion {
    protected int id;
    protected List<Image> images;
    //...

    public class Image {
        private String id;
        private String aspect_ration;

        public Promotion getPromotion() {
            return Promotion.this;    //<-- Always null.
        }
    }
}

The objects of this class are being automatically created and initialized by Gson from json strings.

In the nested class instances, the Promotion.this is always null. Maybe because Gson cannot instantiate it properly. I was thinking to set it manually, but the statement Promotion.this = promotion; causes compile error: Variable expected.

So how can I achieve something like this:

public class Promotion {
    //...

    public class Image {

        public void setPromotion(Promotion promotion) {
            Promotion.this = promotion;   //<-- Is not possible.
        }
    }
}





Aucun commentaire:

Enregistrer un commentaire