4 Replies Latest reply: Feb 28, 2013 1:17 PM by dadams07 RSS

    pass by reference

      Is Java Support pass by reference or not?
        • 1. Re: pass by reference

          Java is always pass by value.


          • 2. Re: pass by reference
            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
              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
              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
                To put it another (and possibly more confusing) way, a method argument is actually a variable holding the "value" of a "reference."