assinging objects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newlearner
    New Member
    • May 2007
    • 67

    assinging objects

    i have the following code


    public class Check_Garbage {
    String a ="Check_Garb age ";
    public static void main(String args[]){
    Check_Garbage c1 = new Check_Garbage() ;
    Check_Garbage c2 = m1(c1);
    Check_Garbage c3 = new Check_Garbage() ;
    c2 = c3;
    }

    static Check_Garbage m1(Check_Garbag e obj1){
    obj1 = new Check_Garbage() ;
    obj1.a="This is a Method Object";
    return obj1;
    }
    }


    My doubt is ...the statement
    c2 = c3 means the methods of c3 are transferred to c2 or is c2 refering to the methods of c3.


    Bacicaly will the methods in c2 work even if c3 is deleted?

    Eagerly Looking forward....
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by newlearner
    My doubt is ...the statement
    c2 = c3 means the methods of c3 are transferred to c2 or is c2 refering to the methods of c3.


    Bacicaly will the methods in c2 work even if c3 is deleted?
    You have a bit of misunderstandin g about what objects really are: when an object
    is actually instantiated (using the 'new' operator) a bit of memory is allocated
    for the member variables of the object; not the static ones, because they belong
    to the class of the object, just the non-static member variables. Plus one single
    refererence: the 'this pointer' that points to the class to which the object belongs.

    The methods of the classs (the static ones) and the methods of the object (the
    non-static ones) are stored somewhere else in memory and are never copied or
    duplicated or whatever.

    In Java your (non-primitive) variables are just pointers to that bit of memory.
    When you assign c2= c3 you just forget about the piece of memory to which
    c2 pointed to just before the assignment. The garbage collector takes care of
    that memory, nothing to worry about.

    After the assignment both c2 and c3 point to the same block of memory. That
    piece of memory has the 'this pointer' so it 'knows' the class to which it belongs.
    Nothing is forgotten along the way and no methods are duplicated etc. etc.

    kind regards,

    Jos

    Comment

    • newlearner
      New Member
      • May 2007
      • 67

      #3
      if c2 and c3 are pointing to the same block of memory... thn, if suppose c3 is deleted by garbage collection . is c2 still a valid object and can the methods be accessed even when c3 is not available.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by newlearner
        if c2 and c3 are pointing to the same block of memory... thn, if suppose c3 is deleted by garbage collection . is c2 still a valid object and can the methods be accessed even when c3 is not available.
        No; after you do 'c2= c3' not only point c2 and c3 to the same object but nothing
        points to the object to which c2 pointed previous to the assignment anymore.
        You can't refer to it anymore so that object will be a eligable for garbage
        collection.

        kind regards,

        Jos

        Comment

        • newlearner
          New Member
          • May 2007
          • 67

          #5
          thts fine... thanks...

          but my concern is after c2=c3... since both are one and the same. will there be a garbage collection on either c2 or c3.

          If c3 is collected is c2 still valid bcoz c2 & c3 are pointing to the same point

          Another doubut too....

          If either c2 or c3 is deleted... will c1 will also be deleted as c1 is also the same as c3 and c2

          Eagerly waiting...

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            You're overcomplicatin g matters; this is what it looks before the assignment:

            Code:
            +--+
            |C1|-------------------------------> OBJECT1
            +--+
            
            +--+
            |C2|-------------------> OBJECT2
            +--+
            
            +--+
            |C3|-----------> OBJECT3
            +--+
            After the assignment c2= c3 it'll look like this:

            Code:
            +--+
            |C1|-------------------------------> OBJECT1
            +--+
            
            +--+
            |C2|----------------+    OBJECT2
            +--+                |
                                |
            +--+                v
            |C3|-----------> OBJECT3
            +--+
            Both c2 and c3 point to the same object and the object c2 pointed to before
            the assignment is stone dead, nothing points to it anymore and the garbage
            collector will wipe it away when necessary.

            The other objects (OBJECT1 and OBJECT3) are still alive because variables still
            point to them and the garbage collector will leave them alone.

            kind regards,

            Jos

            Comment

            Working...