Results 1 to 16 of 16
  1. #1
    katturv is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default static variable /pass by reference

    can a static variable be passed by value/reference in java???

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by katturv View Post
    can a static variable be passed by value/reference in java???
    Everything is passed by value in Java; always.

    kind regards,

    Jos

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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.
    Java Code:
    public int x(int y) {
        y = 5;
        return y;
    }
    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.

    However when passing an object by value, you can modify its properties and the original object will be modified.
    Java Code:
    private class A {
        public int m_B = 2;
    }
    public void x(A y) {
        y.m_B = 7;
    }
    Now, if this was called by "A z = new A(); x(z);", then z.m_B would be 7, not 2.

    More on that here: Does Java pass by reference or pass by value? - JavaWorld

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by Zack View Post
    However when passing an object by value, you can modify its properties and the original object will be modified.
    Java Code:
    private class A {
        public int m_B = 2;
    }
    public void x(A y) {
        y.m_B = 7;
    }
    Now, if this was called by "A z = new A(); x(z);", then z.m_B would be 7, not 2.
    In the expression x(z) what is being passed is a reference value, not an object. The situation might be expressed by saying "when passing a reference by value, you can modify the member variables that are being referenced."

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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).

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by Zack View Post
    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.

    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.

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    A Python function is rather like Java method in this respect.

    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.)

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Zack View Post
    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.
    C also passes all parameters by value; always.

    kind regards,

    Jos

  10. #10
    jac0117 is offline Member
    Join Date
    Jan 2008
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Everything is passed by value in Java; always.

    kind regards,

    Jos
    This is not true. Only primitives are passed by value, objects passed by reference.
    Last edited by jac0117; 10-03-2010 at 02:57 AM.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jac0117 View Post
    This is not true. Only primitives are passed by value, objects passed by reference.
    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.
    Last edited by Fubarable; 10-03-2010 at 03:18 AM.

  12. #12
    jac0117 is offline Member
    Join Date
    Jan 2008
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You are very wrong. EVERYTHING in Java is passed by value. Everything. Please reread the relevant section of your text.
    You are the wrong one buddy.

    MyObject obj = new MyObject();
    obj.setValue(10);
    this.foo(obj);

    public void foo(MyObject o)
    {
    o.setValue(15);
    }

    What happens when you call:
    System.out.println(obj.getValue());

    Does this print 10 or 15?

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jac0117 View Post
    You are the wrong one buddy.
    Did you read the links I provided?

    Do you understand what it means when I say that the reference to the object is passed by value?

    Again, please read the articles before you make yourself look foolish.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Last edited by Fubarable; 10-03-2010 at 03:28 AM.

  15. #15
    jac0117 is offline Member
    Join Date
    Jan 2008
    Posts
    9
    Rep Power
    0

    Default

    Sorry Fubarable, you are right. Wow I can't believe I had gone so long thinking this way. I apologize you are right.

  16. #16
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jac0117 View Post
    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

Similar Threads

  1. Replies: 7
    Last Post: 09-24-2010, 05:09 AM
  2. How to create pass by reference??
    By --> xeiyne! in forum CLDC and MIDP
    Replies: 4
    Last Post: 04-08-2010, 06:43 PM
  3. Don't pass by reference
    By Lyven in forum Advanced Java
    Replies: 6
    Last Post: 11-16-2009, 06:06 PM
  4. Pass-by-reference
    By popjava in forum New To Java
    Replies: 1
    Last Post: 10-19-2009, 03:45 PM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •