0

How can I override private variable by passing it as a variable in a class member function?

For example:

public class SampleActivity extends Activity {
    private ImageView image1 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        this.onUpdate(image1);
    }

    private void onUpdate(ImageView image) {
        image = new ImageView(this); // I would like to override member variable image1
        // update image1 parameters
    }
};

In example above, image1 will not be updated (remains as null).

My implementation:

public class SampleActivity extends Activity {
    private ImageView image1 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        image1 = new ImageView();
        this.onUpdate(image1);
    }

    private void onUpdate(ImageView image) {
        // update image1 parameters
    }
};

In example 2, I will need to initialize every time before onUpdate function. Let's say I would like to dynamically allocate for the private variable, what should I do?

Is there a better implementation for this? (For example, passing "Ref" like C# so it can be overriden?)

morph85
  • 807
  • 10
  • 18
  • 1
    This answer might be useful for you: http://stackoverflow.com/questions/40480/is-java-pass-by-reference – Steven Aug 22 '13 at 07:26
  • Try to use `this.image1 = image1` in your `onUpdate(ImageView image1)` method – Lia Pronina Aug 22 '13 at 07:26
  • 1
    You cannot override variable, only methods are overridden. Although, you can change a variable value in its subclass, unless it is declared private or final. – Malwaregeek Aug 22 '13 at 07:39

3 Answers3

4

In Java, all arguments are passed by value. That means that the value of image1 cannot be changed in the called method; only the local copy of the reference can be changed. You could do something like this:

@Override
protected void onResume() {
    super.onResume();
    image1 = onUpdate();
}

private ImageView onUpdate() {
    ImageView image = new ImageView(this); // I would like to override member variable image1
    // update image parameters
    return image;
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

There is nothing like reference in java.

In this situation you can use getters and setters.

like

public image getImage()
{

return image;
}

public void setImage(Image image)
{
this.image=image;
}
Rajnish Mishra
  • 826
  • 5
  • 21
0

If i understand you correctly, you do not need to have input parameter to your onUpdate since you already have a reference delcared (private ImageView image1 = null;). Try something like this:

public class SampleActivity extends Activity {
private ImageView image1 = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
protected void onResume() {
    super.onResume();
    this.onUpdate();
}

private void onUpdate() {
    image = new ImageView(this); // I would like to override member variable image1
    // update image1 parameters
    image.invalidate(); //invalidate the imageview so that the changes take place.
}
};
AggelosK
  • 4,313
  • 2
  • 32
  • 37
  • I did this because I have a few member variable to be allocated dynamically. I was trying to reduce redundancy in the code. – morph85 Aug 22 '13 at 07:35