2

I have a method which takes in 4 floats and modifies the value. Now I would like to set the floats of the parent class to the value that the method took in. Some code:

public void influence(float x, float y, float w, float h){
x += 5; y += 5; w += 5; h += 5;
}

Now that would be called from the parent class like this:

float x = 5, y = 5, w = 5, h = 5;
influence(x, y, w, h);

I would like to set the floats in the parent to the modified method. I know I could return a float[] and then manually set the floats of the parent class to that array but is there a better way to do this? I could also use a class like Rect but that would be like an array.

4
  • 4
    You can't modify the callers values like that (and Java has no pointers). You could pass in an array of floats or you could return an array of floats (or a POJO) because that is passing the value of an Object reference. Commented Aug 23, 2014 at 0:55
  • You can't change parameters in Java, since all parameter forwarding in Java is by value. What you can do is either give POJO object to that method which will update it (do object.setX(object.getX()+5)), or I think better by design is that your object has method like object.influence(), which will change it's fields x, y, w, h... Commented Aug 23, 2014 at 1:01
  • I know but i was just wondering if there was ANY way to do it. Currently I am returning a float[]. Commented Aug 23, 2014 at 1:02
  • @TameHog No. You cannot change the value of primitives that are passed in. Commented Aug 23, 2014 at 1:20

2 Answers 2

2

Java does not have a way to pass a reference to a primitive. However, in your situation it would probably be better to avoid it anyway: it appears that the four variables are related to each other.

When several variables are related, they should be instance variables of a single object. Your object probably looks like a rectangular box that has the origin of (x, y), height h, and width w. Make a class for it, and let the method change that class, or return a new instance:

class Rect {
    private final float x, y, w, h;
    public float getX() { return x; }
    public float getY() { return y; }
    public float getW() { return w; }
    public float getH() { return h; }
    public Rect(float x, float y, float w, float h) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
    }
}

Then you can write a method returning a new Rect

public Rect influence(Rect inputRect) {
    ...
}

or make Rect mutable, and let influence modify its values.

Sign up to request clarification or add additional context in comments.

Comments

1

You can't change primitives in Java like that because everything in Jave is passed by value - but you can store the value in an array and pass it:

public void influence(float[] arr){
    for (int i=0; i<arr.length; i++){
        arr[i] += 5;
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.