Results 1 to 4 of 4
  1. #1
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default hi guys , please help me in telling about how Objects are passed by reference. :(

    Java Code:
    // Objects are passed by reference.
    class Test {
    int a, b;
    Test(int i, int j) {
    a = i;
    b = j;
    }
    
    // pass an object
    void meth(Test o) {
    o.a *= 2;
    o.b /= 2;
    }
    }
    class CallByRef {
    public static void main(String args[]) {
    Test ob = new Test(15, 20);
    System.out.println("ob.a and ob.b before call: " +
    ob.a + " " + ob.b);
    ob.meth(ob);
    System.out.println("ob.a and ob.b after call: " +
    ob.a + " " + ob.b);
    }
    }
    here i am getting doubt s

    Java Code:
    ob.meth(ob);
    1."how a object can send itself to its own method as a reference " ?

    2. how this is happening ?

    3.what is going behind this ?

    4. is this happening by handle(which is beyond pointer) ?

    sorry for my nobbie question ,but just wanted to know clearly about the process happening in JVM (-:
    Last edited by funkygarzon; 06-14-2011 at 11:37 AM.

  2. #2
    besil is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    hi my friend,
    it is pretty simple as you know all parameters in java are passed by value and in a case of objects the reference is passed by value
    in your example the calling "ob.meth(ob)" cause at the object in the function "meth" to create another reference (if it is more convinient to you think of it as a pointer) called o and now both o and ob are pointing to the same object i.e ob so at this point think that this method can change a data such as a and b with the offset :-)

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,252
    Rep Power
    19

    Default

    hi guys , please help me in telling about how Objects are passed by reference. :(
    They aren't. All parameters in Java are passed by value. There's an excellent article about that here: JavaRanch Campfire - Pass By Value, Please

    Don't ignore the tip in the first paragraph.

    db

    db

  4. #4
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by DarrylBurke View Post
    They aren't. All parameters in Java are passed by value. There's an excellent article about that here: JavaRanch Campfire - Pass By Value, Please

    Don't ignore the tip in the first paragraph.

    db

    db
    Quote Originally Posted by besil View Post
    hi my friend,
    it is pretty simple as you know all parameters in java are passed by value and in a case of objects the reference is passed by value
    in your example the calling "ob.meth(ob)" cause at the object in the function "meth" to create another reference (if it is more convinient to you think of it as a pointer) called o and now both o and ob are pointing to the same object i.e ob so at this point think that this method can change a data such as a and b with the offset :-)
    WOOOOOOW amazing DarrylBurke and besil,

    I clearly understood the concept behind pass by reference and pass by value ...thanks a lot

    especially DarrylBurke , that was the awesome link which made me clearly understand what is happening behind pass by reference and pass by value in JVM :) thanks a lot buddy :)

Similar Threads

  1. compiler telling me to make a file for a public class
    By silverglade in forum New To Java
    Replies: 10
    Last Post: 04-26-2011, 07:47 AM
  2. Max size for a passed parameter?
    By ravl10 in forum New To Java
    Replies: 3
    Last Post: 01-29-2010, 06:27 PM
  3. entities are passed by value or passed by reference
    By syntrax in forum New To Java
    Replies: 1
    Last Post: 12-17-2009, 07:13 AM
  4. java certification passed
    By natasha in forum Java Certification
    Replies: 0
    Last Post: 12-05-2009, 08:24 AM
  5. Cannot get passed these syntax errors
    By MrKP in forum New To Java
    Replies: 1
    Last Post: 05-12-2008, 07:05 AM

Posting Permissions

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