This discussion is archived
4 Replies Latest reply: Feb 28, 2013 11:17 AM by aksarben RSS

pass by reference

993570 Newbie
Currently Being Moderated
Is Java Support pass by reference or not?
  • 1. Re: pass by reference
    789895 Expert
    Currently Being Moderated
    Hi,

    Java is always pass by value.

    cheers,

    VT
  • 2. Re: pass by reference
    gimbal2 Guru
    Currently Being Moderated
    I don't get it. This has been asked a million times and the answer is always the same - any google about the subject will point to endless forum posts saying exactly the same. There is no need to question it anymore at this point in time. So why does it persist anyway? Which evil source is casting doubts into people's minds?
  • 3. Re: pass by reference
    rp0428 Guru
    Currently Being Moderated
    Welcome to the forum!
    >
    Is Java Support pass by reference or not?
    >
    No - see my detailed reply in thise thread
    Java passing by reference, where a good detailed explaination?

    That reply includes quotes from the Java Language Specification as well as links to articles that provide examples. This is from the spec
    http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.1
    >
    See section 8.4.1
    When the method or constructor is invoked (§15.12), the values of the actual argument expressions initialize newly created parameter variables, each of the declared type, before execution of the body of the method or constructor.
    Note that the above applies to argument expressions of ALL types: primitive or object. The value of the 'newly created parameter variable' is a COPY (by value) of the actual argument expression.

    So a method that has an object parameter can use the 'copy' of the object reference to manipulate the object the method can NOT change the reference to refer to a different object.
  • 4. Re: pass by reference
    aksarben Journeyer
    Currently Being Moderated
    To put it another (and possibly more confusing) way, a method argument is actually a variable holding the "value" of a "reference."

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points