Error with super(this) when calling a Java constructor

Asked

Viewed 120 times

5

This is allowed?

public Produto(Context context) {
    super(context, this);   
}

You’re making a mistake :

Cannot refer to 'this' nor 'super' while explicitly invoking a constructor

I’m trying to make a simple MVC for when I type produto.Save() i manage to rescue the values that were set below:

Produto produto1 = new Produto(this);
    produto1.setId(1);
    produto1.setNome("CELULAR");
    produto1.setPreco(5.5);
    produto1.Save();

The source of it lies in this other doubt: Android getDeclaredFields value

  • 2

    You’re trying to pass this at a time when the object is still being constructed and therefore not ready to be referenced.

1 answer

4


It is not necessary (and not allowed) to pass a reference to the object itself in its constructor.

To refer to yourself in the superclass, just use this, in the same way as in the subclass.

No matter at which level of the class hierarchy you are coding, this will always refer to the instance that was created.

  • So I’m missing the reference in the code when I try to access: Object b = idField.get(get().getConstructor(Context.class).newInstance(this.myContext)); it returns me 0 instead of the value I set in activityMain

Browser other questions tagged

You are not signed in. Login or sign up in order to post.