DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Aristotelis Guest

    Holding a reference in JAVA


    I 'm just not sure on the way java can keep a reference on an object.
    If I have the following code:

    MyClass A = new MyClass();
    MyClass B = null;
    B = A;
    Does B hold a reference on A or an instance of it?And consequently, if I
    change B afterwards, will this affect A?
    thanks
    aik_b

  2. #2
    Aristotelis Guest

    Re: Holding a reference in JAVA


    "Aristotelis" <ykoto@ced.tuc.gr> wrote:
    >
    >I 'm just not sure on the way java can keep a reference on an object.
    >If I have the following code:
    >
    >MyClass A = new MyClass();
    >MyClass B = null;
    >B = A;
    >Does B hold a reference on A or an instance of it?And consequently, if I
    >change B afterwards, will this affect A?
    >thanks
    >aik_b


    Do you Have any idea of the way you could do such think??

  3. #3
    Guest

    Re: Holding a reference in JAVA


    "Aristotelis" <a@a.com> wrote:
    >
    >"Aristotelis" <ykoto@ced.tuc.gr> wrote:
    >>
    >>I 'm just not sure on the way java can keep a reference on an object.
    >>If I have the following code:
    >>
    >>MyClass A = new MyClass();
    >>MyClass B = null;
    >>B = A;
    >>Does B hold a reference on A or an instance of it?And consequently, if

    I
    >>change B afterwards, will this affect A?
    >>thanks
    >>aik_b

    >
    >Do you Have any idea of the way you could do such think??


    This problem is covered in Appendix A of Bruce Eckel's free online book,
    'Thinking in Java' (3rd edition), which you can download from http://www.bruceeckel.com.
    That Appendix is titled 'Passing and Returning Objects'.

    Essentially Java doesn't make 'deep copies' and instead holds a reference,
    so in your example, if you later change A, B will also change (changing B
    should not affect A however). There is a method called Object.clone() which
    Eckel also talks about, and a method for 'deep copying' arrays.



  4. #4
    Jay Guest

    Re: Holding a reference in JAVA


    <java.@127.0.0.1> wrote:
    >
    >"Aristotelis" <a@a.com> wrote:
    >>
    >>"Aristotelis" <ykoto@ced.tuc.gr> wrote:
    >>>
    >>>I 'm just not sure on the way java can keep a reference on an object.
    >>>If I have the following code:
    >>>
    >>>MyClass A = new MyClass();
    >>>MyClass B = null;
    >>>B = A;
    >>>Does B hold a reference on A or an instance of it?And consequently, if

    >I
    >>>change B afterwards, will this affect A?
    >>>thanks
    >>>aik_b

    >>
    >>Do you Have any idea of the way you could do such think??

    >
    >This problem is covered in Appendix A of Bruce Eckel's free online book,
    >'Thinking in Java' (3rd edition), which you can download from http://www.bruceeckel.com.
    >That Appendix is titled 'Passing and Returning Objects'.
    >
    >Essentially Java doesn't make 'deep copies' and instead holds a reference,
    >so in your example, if you later change A, B will also change (changing

    B
    >should not affect A however). There is a method called Object.clone() which
    >Eckel also talks about, and a method for 'deep copying' arrays.
    >
    >

    Am i missing something here?
    Both A and B are references to a object of type MyClass. If you must you
    can think of A and B as pointers of type Myclass* in C++. Neither A nor B
    is special, neither actually contains the underlying space (memory) associated
    with a MyClass object. Thus 'changing B' and 'changing A' mean the same thing
    - change the object (memory) referenced by B (or A) and since both A and
    B refer to the same object a 'change to B' is the obviously seen as a 'change
    to A'.

    The JVM is responsible for keeping track of all the references to an object
    - when all the references become inaccessible the underlying object becomes
    'garbage' and its associated space (memory) is made available for some other
    object (i.e., it is garbage collected).


  5. #5
    Mike Guest

    Re: Holding a reference in JAVA


    <java.@127.0.0.1> wrote:
    >
    >"Aristotelis" <a@a.com> wrote:
    >>
    >>"Aristotelis" <ykoto@ced.tuc.gr> wrote:
    >>>
    >>>I 'm just not sure on the way java can keep a reference on an object.
    >>>If I have the following code:
    >>>
    >>>MyClass A = new MyClass();
    >>>MyClass B = null;
    >>>B = A;
    >>>Does B hold a reference on A or an instance of it?And consequently, if

    >I
    >>>change B afterwards, will this affect A?
    >>>thanks
    >>>aik_b

    >>
    >>Do you Have any idea of the way you could do such think??

    >
    >This problem is covered in Appendix A of Bruce Eckel's free online book,
    >'Thinking in Java' (3rd edition), which you can download from http://www.bruceeckel.com.
    >That Appendix is titled 'Passing and Returning Objects'.
    >
    >Essentially Java doesn't make 'deep copies' and instead holds a reference,
    >so in your example, if you later change A, B will also change (changing

    B
    >should not affect A however). There is a method called Object.clone() which
    >Eckel also talks about, and a method for 'deep copying' arrays.
    >
    >

    Sorry, but that's wrong - changing B does affect A. They both point to the
    same instance.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links