• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

General question on a static class variable object type

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I became quite curious after understanding the answer to a garbage collection question, so Im not asking about garbage collection here, because I know the answer and roughly understand the answer, and Ive seen this same question time again on many past posts in this forum.

My question centres around static class variable of an object type, not a primitive.



So I know you have all seen this same code snippet before, but what I want to know here is, how does the Beta b1 = new Beta() become static Beta b1 inner value for alpha a2. I can see it gets set for a1, as in a1.b1 = b1. Im assuming that once defined, a class can only have one reference to a static object which all other classes share, but I wouldnt naturally assume, that a2.b1 gets assigned this value just because a1 has it set. I further guestimate that the compiler just assumes, that because this is the static value set for a1, then it might as well "fill in the blanks" for b2. Would I assume this correctly?

Thanks for your time to read this, and again, thank you for your help.

 
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess is 4 objects qualify for GC

a1,a1.b2,b1, b2

a1.b1 is not marked for GC, as it is shared by a2 .

I'm not sure, though.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Synes wrote: Ive seen this same question time again on many past posts in this forum.



Regardless... please QuoteYourSources.

 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you take a look at these two lines of the code...

Ben Synes wrote:



you will see that four objects have been created -- using the "new" operator. So... hint...

Kumar Raja wrote:My guess is 4 objects qualify for GC

a1,a1.b2,b1, b2

a1.b1 is not marked for GC, as it is shared by a2 .



So, you are saying that all four objects, that have been created, are eligible for GC?

Remember that it is objects that are eligible for GC -- not references. You need to distinguish between the two when addressing these types of questions.

Henry
 
Ben Synes
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys!

My question is not how many objects are candidates for garbage collection, I know the answer the answer is 1, a1. But my QUESTION is, how does a2s Beta b1 get set to be b1?? Please read my post again carefully.

The reference for this material is the K&B SCJP 6 self study book, page 285.

 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Synes wrote:
My question is not how many objects are candidates for garbage collection, I know the answer the answer is 1, a1. But my QUESTION is, how does a2s Beta b1 get set to be b1?? Please read my post again carefully.




Hint: Is there a difference between "a1.b1" and "a2.b1"? Or do they both get converted to "Alpha.b1"?

Henry
 
Kumar Raja
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,



you will see that four objects have been created -- using the "new" operator. So... hint...

Kumar Raja wrote:My guess is 4 objects qualify for GC

a1,a1.b2,b1, b2

a1.b1 is not marked for GC, as it is shared by a2 .



So, you are saying that all four objects, that have been created, are eligible for GC?



No, I'm not saying that a1,a2,b1 and b2 will be GC. I'm saying a1 is GC, because it is now pointing to null. Thus also b2, since it is part of a1. Similarly b1 and b2 are GC, because they are explicitly pointed to null. This makes a total of 4 . a1.b2 is not GC, even though a1 = null, because b2 is also associated with a2 which is not null yet.


Am I wrong ?

I think, I realized my mistake.

Given the a1 = null, b1 = null, b2 = null

but since b1 -> a1.b1 and b1 ->a1.b2 i.e a1.b1 and a1.b2 point to the same object as b1 is pointing and since a1.b1 being a static and at class level it is shared with a2.b1

even though b1 = null, a1 = null, but since a2 != null, a2.b1 still holds the pointer to the same object as a1.b1. So this makes b1 still alive. and so the case with a1.b2.

So the only object that is GC is a1.

Is my understanding correct ?



Ben, why not b1 and b2 GC'ed. They are created by new and also now made null.
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kumar Raja wrote:
I think, I realized my mistake.

Given the a1 = null, b1 = null, b2 = null

but since b1 -> a1.b1 and b1 ->a1.b2 i.e a1.b1 and a1.b2 point to the same object as b1 is pointing and since a1.b1 being a static and at class level it is shared with a2.b1

even though b1 = null, a1 = null, but since a2 != null, a2.b1 still holds the pointer to the same object as a1.b1. So this makes b1 still alive. and so the case with a1.b2.

So the only object that is GC is a1.




Basically, yes. When working through these types of questions, you need to track the objects, and whether they are reachable or not. Setting a reference to null, does not necessarily mean that an object is eligible for GC.



BTW, I liked that you did a strike out of your previous response, instead of just replacing it -- I granted you a cow...

Henry
 
Kumar Raja
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the cow.
 
Ben Synes
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Hint: Is there a difference between "a1.b1" and "a2.b1"? Or do they both get converted to "Alpha.b1"?



Yep I got it, now it all seems to clear. But this would not be the case if Beta b1 = new Beta() had not been called. Before that point, the class variable b1 in Alpha would be a reference to null I presume?
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Synes wrote:
Yep I got it, now it all seems to clear. But this would not be the case if Beta b1 = new Beta() had not been called. Before that point, the class variable b1 in Alpha would be a reference to null I presume?



The static variable, b1, of the Apha class -- is still referencing null, even after that point. The point that you are mentioning declares a local variable named b1, and is different from the b1, that is the static variable of the Alpha class.

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic