tags:

views:

1025

answers:

5

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?

+3  A: 

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
You are assuming that getCookie() returns the actual object rather than a copy.
jdigital
See my response on the accepted answer.
Kieron
A: 

Java objects are always passed by value. So if you change value of coolie (for example cookie = null) you will not change original cookie.

del-boy
This is absolutely false. Please do some basic background checks before you make authoritative statements like this. True, if you set cookie = null, you null the reference not the object. Look at the accepted answer to see how this really works in Java.
Einar
+9  A: 

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:

Zach Scrivena
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
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
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
@jdigital: I agree and that is why I qualified my statement with "if you change the state of the object".
Zach Scrivena
@Kieron: You're right, the object I was referring to is whatever was returned by "request.getCookie()".
Zach Scrivena
A: 

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
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
A: 

object reference is different from object. for eg: class Shape { int x1=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); } }

kalyani
correct answer.
kalyani