Results 1 to 6 of 6
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default should I consult a shrink

    The next code, again from the Thinking in Java book, I find very confusing! Who can help understanding things better?

    It is about three classes and the last class, RefenrenceCounting, just puts the things at work:

    The author says "The static long counter keeps track of the number of instances of Shared"......

    But the object "shared" of the class "Shared" gets only created ones (see line 38) and what happens next is that the members of the new created array of the type "Composing" (see line 39), create the object "composing" four times which are the numbers 1 to 4 of the output (see bottom part of the code), right?

    "Shared" is created once but its reference "shared" is passed four times as an argument of the created "composing" objects. See for instance the long variable "counter" (counter = 0 and id = counter++) PRESENT IN BOTH CLASSES SHARED AND COMPOSING!

    Question: why is the first printed "id" (first output line) 0 and not 1, since the created "share" object contains counter++ which adds immediately the counter with one value, right?

    So the numbers of the output list (last part of the code) are 0, 0, 1, 2, 3, 4, of which the first zero is of the Shared id and the next numbers of the Composing id, right?

    This stuff makes me dizzy!
    -should programming be like this?
    -should I consult a shrink
    -or should I look for something else in life?



    Java Code:
    //: polymorphism/ReferenceCounting.java 
    // Cleaning up shared member objects. 
    import static net.mindview.util.Print.*; 
     
    class Shared { 
      private int refcount = 0; 
      private static long counter = 0; 
      private final long id = counter++; 
      public Shared() { 
        print("Creating " + this); 
      } 
    public void addRef() { refcount++; } 
      protected void dispose() { 
        if(--refcount == 0) 
          print("Disposing " + this); 
      } 
      public String toString() { return "Shared " + id; } 
    } 
     
    class Composing { 
      private Shared shared; 
      private static long counter = 0; 
      private final long id = counter++; 
      public Composing(Shared shared) { 
        print("Creating " + this); 
        this.shared = shared; 
        this.shared.addRef(); 
      } 
      protected void dispose() { 
        print("disposing " + this); 
        shared.dispose(); 
      } 
      public String toString() { return "Composing " + id; } 
    } 
     
    public class ReferenceCounting { 
      public static void main(String[] args) { 
        Shared shared = new Shared(); 
        Composing[] composing = { new Composing(shared), 
          new Composing(shared), new Composing(shared), 
          new Composing(shared), new Composing(shared) }; 
        for(Composing c : composing) 
          c.dispose(); 
      } 
    }
    Output:
    Creating Shared 0
    Creating Composing 0
    Creating Composing 1
    Creating Composing 2
    Creating Composing 3
    Creating Composing 4
    disposing Composing 0
    disposing Composing 1
    disposing Composing 2
    disposing Composing 3
    disposing Composing 4
    Disposing Shared 0

  2. #2
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: should I consult a shrink

    Maybe I should I ask myself first what the counter is really doing when one says:
    1) int counter = 0;
    2) counter++
    3) print counter

    will it print 0 or 1?

    ok lets pull it through the compiler....

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: should I consult a shrink

    Just talking to myself (might need a shrink)

    Same as C
    in Count++ first it will use initial value and then increase it with one
    in ++Count it will increase with one and then it will use it in loop.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default Re: should I consult a shrink

    Please go through the Forum Rules, particularly the third paragraph.

    db
    Last edited by Fubarable; 02-11-2013 at 05:35 AM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: should I consult a shrink

    What really does not make any sense to my are the next few code lines,
    lines 7 and 8 of the above code

    private static long counter = 0;
    private final long id = counter++;

    with each object, created of that class, Shared, see the above code,
    the variable "id" will always be zero, right?

  6. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: should I consult a shrink

    Ok got it, everywhere one sees "id" one should read count++,
    sorry for asking.

Similar Threads

  1. Expand & Shrink Buttons
    By umaza in forum AWT / Swing
    Replies: 1
    Last Post: 10-31-2012, 04:22 AM
  2. Dispatch messages system design consult
    By rayman in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 12-04-2011, 10:19 AM
  3. Replies: 9
    Last Post: 09-11-2011, 05:27 PM
  4. shrink and expand nemu when user click
    By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-06-2009, 07:02 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
  •