can a static variable be passed by value/reference in java???
Printable View
can a static variable be passed by value/reference in java???
It is important to distinguish exactly what passing by value does.
When you pass an integer to a function, it will receive ONLY the value of that integer.
If called as "int a = 6; int b = x(a);", then b will be 5 and a will be 6. That is being passed by primitive value.Code:public int x(int y) {
y = 5;
return y;
}
However when passing an object by value, you can modify its properties and the original object will be modified.
Now, if this was called by "A z = new A(); x(z);", then z.m_B would be 7, not 2.Code:private class A {
public int m_B = 2;
}
public void x(A y) {
y.m_B = 7;
}
More on that here: Does Java pass by reference or pass by value? - JavaWorld
You can call it a reference value, but it's still being passed by value. Setting the value of y in the x() function would not have an effect on the z variable passed to it, so strictly it's not a reference--it's still a value (which you may call a reference value if you want to be picky, but it's a value regardless).
I was being picky, but only because I agree with the sentiment expressed in reply #3 that it's important to understand what's going on.
My difference of opinion is not with what the passing sematics is: it is always "by value" in Java. Rather I was trying to suggest that the code that was given did not illustrate an object being passed.
Yes, I can call what was being passed a reference value. And I will do so ... because it is a reference value and it is not an object.
Quote:
4.1 The Kinds of Types and Values
There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3). There are, correspondingly, two kinds of data values that can be stored in variables, passed as arguments, returned by methods, and operated on: primitive values (§4.2) and reference values (§4.3).
(JLS 4.1)
-----
I have no idea what causes the perennial myth that there might be some sort of argument passing in Java other than passing by value. But it does not help to talk about objects being passed as people are apt to see the state of these objects change in the caller's context and erroneously conclude that some other semantics are involved.
The reason a lot of people assume Java can handle passing by reference is because common OOP languages can--C, C#, C++, Python (I think), PHP... the list goes on. Its absence in Java is actually odd to me as well; but it's a fact of programming that we get to deal with. :)
As for the semantics, I think people need to understand what passing by value is. It doesn't mean that any object you pass cannot be changed by the function. Rather, the data it points to is what cannot be changed; the data contained IN the data that it points to can. It's a bit difficult to explain, particularly with broadness of definition in mind, so the examples are about the best I can do.
A Python function is rather like Java method in this respect.
Quote:
The actual parameters (arguments) to a function call are introduced in the local symbol table of the called function when it is called; thus, arguments are passed using call by value (where the value is always an object reference, not the value of the object).
(The Python Tutorial 4.6 Defining Functions. In a footnote the author speculates about the expression "call by object reference" thereby reintroducing confusion which the tutorial itself eliminated: at issue is not how the call passes things but what the things are that are being passed)
C is very close to Java with the latter's "reference values" finding a close homologue with the values of pointers. (So close that Java happily describes an unexpected null value of these things as a NullPointerException). As in Java the caller of a C function can see changes in the thing pointed to by a pointer argument, but the pointer is the thing passed and it is passed by value: ie no assignment to the formal parameter within the function will be visible to the caller.
-----
Another possible source of confusion is that people see the word "reference" in the expressions "reference type" and "reference value" (which are proper to the Java programming language) and jump to the conclusion that they are something like references in PHP, and the other languages you mention, which are used to give a quite different meaning to assigning, passing and returning things. (As that link explains with examples that have no direct counterparts in Java.)
You are very wrong. EVERYTHING in Java is passed by value. Everything. Please reread the relevant section of your text.
Also see:
Java is Pass by Value, Dammit!
JavaWorld: Does Java pass by reference or pass by value?
Also, I agree with Norm in your other post: please don't give students full code solution as you cheat them out of the value of creating their own code and thereby learning.
And more articles:
JavaRanch version
A college text version
edit, and more still:
FAQ-JAVA-Java-uses-Pass-by-Reference-Or-Pass-by-Value
stackoverflow:is-java-pass-by-reference
Sorry Fubarable, you are right. Wow I can't believe I had gone so long thinking this way. I apologize you are right.
The most authoritative quote I could find: "There is exactly one parameter passing mode in Java - pass by value - and that helps keep things simple." The Java Programming Language, 2nd ed. by Ken Arnold and James Gosling, section 2.6.1, page 40, 3rd paragraph.
Here's a link.
The trouble is that Java uses the exact same parameter passing mechanism as C does but it hides all pointer stuff from the programmer so you can't see what's happening anymore (it all happens behind the scene).
kind regards,
Jos