Results 1 to 6 of 6
Thread: should I consult a shrink
- 02-10-2013, 08:42 AM #1
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 609
- Rep Power
- 6
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?
Output: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(); } }
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
- 02-10-2013, 08:51 AM #2
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 609
- Rep Power
- 6
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....
- 02-10-2013, 08:56 AM #3
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 609
- Rep Power
- 6
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.
- 02-11-2013, 05:31 AM #4
Re: should I consult a shrink
Please go through the Forum Rules, particularly the third paragraph.
dbLast edited by Fubarable; 02-11-2013 at 05:35 AM.
Why do they call it rush hour when nothing moves? - Robin Williams
- 02-11-2013, 12:17 PM #5
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 609
- Rep Power
- 6
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?
- 02-11-2013, 12:22 PM #6
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 609
- Rep Power
- 6
Similar Threads
-
Expand & Shrink Buttons
By umaza in forum AWT / SwingReplies: 1Last Post: 10-31-2012, 04:22 AM -
Dispatch messages system design consult
By rayman in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 12-04-2011, 10:19 AM -
is there any way to optimize/shrink this code even more with same results?
By just_in_deed in forum New To JavaReplies: 9Last Post: 09-11-2011, 05:27 PM -
shrink and expand nemu when user click
By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 03-06-2009, 07:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks