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?
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With