1

In the past week i have been using function like these too much

function Rectangle setRectangle(Rectangle rect){
    rect = new Rectangle(p,q,r,s);
    return rect;
}

Rectangle rect;
rect = setRectangle(rect);

I just want to do

function void setRectangle(Rectangle rect){
    rect = new Rectangle(p,q,r,s);
} 

Rectangle rect;
setRectangle(rect);

I know there are pointers in c++ in which we can just set the pointer to whatever in the function and it is set in the real variable. I want to know how I can do it in Java.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
usama8800
  • 893
  • 3
  • 10
  • 20

5 Answers5

0

You have references instead of pointers in Java. They let you modify an object to which you have a reference. For example, then you pass a Rectangle to setRectangle, the method could call mutating members of the rect object, changing the object that has been passed in:

static void setRectangle(Rectangle rect){
    rect.Top = p;
    rect.Left = q;
    rect.Width = r;
    rect.Height = s;
}
static void main(String[] args) {
    Rectangle r = new Rectangle(a, b, c, d);
    // At this point r is defined as {a,b,c,d}
    setRectangle(r);
    // At this point r is defined as {p,q,r,s}
}

Note that this would not work if you pass null to rect: although the object is passed by reference, the reference is passed by value.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • will this work `rect = null; rect = new Rectangle(1,2,4,5);`?? – usama8800 Aug 22 '13 at 15:35
  • @usama8800 No, that would not change the original reference, because the reference itself is passed by value. Changes to the reference done inside the function do not reflect on the reference that you pass in, much in the same way as changes to a pointer-typed parameter that you pass in C/C++ do not reflect on the pointer that you passed into the function. You need a pointer to pointer for that - there's no built-in construct like that in Java. – Sergey Kalinichenko Aug 22 '13 at 15:39
  • @usama8800 You can build your own object that wraps a rectangle, and lets you change the reference to rectangle inside it. But there's nothing built in that would work in that way. – Sergey Kalinichenko Aug 22 '13 at 15:41
0

You should take a look at this post about how java passes by reference : Is Java "pass-by-reference" or "pass-by-value"?

The java way to do this is:

void setRectangle(Rectangle rect){
    rect = new Rectangle(p,q,r,s);
} 

Rectangle rect = setRectangle(rect);

THis is because you are newing up a new rectangle within the setRectangle function.

However you can modify the rectangle passed in like so:

void setRectangle(Rectangle rect){
    rect.setP(p);
    rect.setQ(q);
    ...
} 

Rectangle rect;
setRectangle(rect);
Community
  • 1
  • 1
immulatin
  • 2,118
  • 1
  • 12
  • 13
0

What's the intention of the method? The set prefix is certainly misleading, you should pick one that reflects what the method is actually intended for.

If it's to create a rectangle, then it's a factory method:

Rectangle createRectangle() {
  return new Rectangle(p,q,r,s);
}

If you want to (re)configure an existing instance:

void configure(Rectangle rect) {
  rect.x=p;
  rect.y=q;
  rect.width=r;
  rect.height=s;
}
Peter Walser
  • 15,208
  • 4
  • 51
  • 78
0

Every time you create an object in Java, you're actually creating a pointer to the object. But, unlike c/c++ Java doesn't have pointer arithmetic, which I suppose you were looking for. And every time you pass an object, you're actually passing a pointer to it(reference).

But that code you wrote doesn't look like Java, since Java doesn't have function keyword. Also what you have written looks like it should do the same job as constructor. But the thing I don't understand, is from where did you get p,q,r,s? You could just do:

Rectangle rect = new Rectangle(anotherRectangle) //where anotherRectangle is some other rectangle you have previously defined.

or

Rectangle rect = new Rectangle(int width, int height)

or

Rectangle rect = new Rectangle(int x, int y, int width, int height)

or

Rectangle rect = new Rectangle(someDimension) //where someDimension is some other dimension you have previously defined.
Luke
  • 1,284
  • 1
  • 12
  • 34
0

Since you mention C++, it would be instructive to translate your code into the equivalent in C++, so you really understand what it says:

Rectangle *setRectangle(Rectangle *rect) {
    rect = new Rectangle(p,q,r,s);
    return rect;
}

Rectangle *rect;
rect = setRectangle(rect);
newacct
  • 119,665
  • 29
  • 163
  • 224