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?

3
  • 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)? Commented Feb 2, 2009 at 5: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". Commented Feb 2, 2009 at 22:10
  • possible duplicate of Is Java "pass-by-reference" or "pass-by-value"? Commented Apr 30, 2014 at 12:32

4 Answers 4

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:

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

5 Comments

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

1 Comment

You are assuming that getCookie() returns the actual object rather than a copy.
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)

1 Comment

I suggest you edit your answer. There is no "request()"; "request" is an object. The request.getCookie() returns a reference to a cookie object.
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); } } 

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.