4

consider this simple servlet sample:

protected void doGet(HttpServletRequest request, HttpServletResponse response){
    Cookie cookie = request.getCookie();
    // do weird stuff with cookie object
}

I always wonder.. if you modify the object cookie, is it by object or by reference?

allan
  • 29
  • 1
  • 1
  • 5
  • What do you mean by "modify the object cookie"? Are you saying cookie = xxx, or are you actually changing the object itself e.g. cookie.setProperty(xxx)? – Zach Scrivena Feb 02 '09 at 05:25
  • @allan, this is a poorly worded question. "By value" or "By reference" are ways of passing parameters in a method call. Those terms don't really apply when you're talking about modifying an object. And I assume you mean "By Value" rather than "By Object". – jdigital Feb 02 '09 at 22:10
  • possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Raedwald Apr 30 '14 at 12:32

4 Answers4

10

if you modify the object cookie, is it by object or by reference?

Depends on what you mean by "modify" here. If you change the value of the reference, i.e. cookie = someOtherObject, then the original object itself isn't modified; it's just that you lost your reference to it. However, if you change the state of the object, e.g. by calling cookie.setSomeProperty(otherValue), then you are of course modifying the object itself.

Take a look at these previous related questions for more information:

Community
  • 1
  • 1
Zach Scrivena
  • 29,073
  • 11
  • 63
  • 73
  • Your statement "...then of course you are modifying the object itself" is misleading. Of course you are modifying SOME object, but without the code/documentation for "getCookie()" you don't know if you're getting the actual cookie or just a copy of it. – jdigital Feb 02 '09 at 22:02
  • It is the actual cookie; (there's actually no getCookie() method, but rather getCookies(), but we'll excuse that as a pseudocode shortcut). The documentation of getCookies() states "Returns an array containing all of the Cookie objects the client sent with this request." with no mention of copying. – Kieron Feb 02 '09 at 22:55
  • In any case, if it were a copy then we could have no expectation of being able to modify the original object, regardless of whether it was passed by value or by reference. – Kieron Feb 02 '09 at 22:56
  • @jdigital: I agree and that is why I qualified my statement with "if you change the state of the object". – Zach Scrivena Feb 03 '09 at 02:03
  • @Kieron: You're right, the object I was referring to is whatever was returned by "request.getCookie()". – Zach Scrivena Feb 03 '09 at 02:08
3

Java methods get passed an object reference by value. So if you change the reference itself, e.g.

cookie = new MySpecialCookie();

it will not be seen by the method caller. However when you operate on the reference to change the data the object contains:

cookie.setValue("foo");

then those changes will be visible to the caller.

Kieron
  • 11,588
  • 5
  • 34
  • 29
0

object reference is different from object. for eg:

class Shape
{
    int x = 200;
}

class Shape1
{
    public static void main(String arg[])
    {
        Shape s = new Shape();   //creating object by using new operator
        System.out.println(s.x);
        Shape s1;                //creating object reference
        s1 = s;                  //assigning object to reference
        System.out.println(s1.x);
    }
}
Dan
  • 147
  • 14
0

In the following line of code

Cookie cookie = request.getCookie(); /* (1) */

the request.getCookie() method is passing a refrence to a Cookie object.

If you later on change cookie by doing something like

cookie = foo_bar(); /* (2) */

Then you are changing the internal refrence. It in no way affects your original cookie object in (1)

If however you change cookie by doing something like

cookie.setFoo( bar ); /* assuming setFoo changes an instance variable of cookie */

Then you are changing the original object recieved in (1)

hhafez
  • 38,949
  • 39
  • 113
  • 143
  • I suggest you edit your answer. There is no "request()"; "request" is an object. The request.getCookie() returns a reference to a cookie object. – jdigital Feb 02 '09 at 22:08