• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Garbage collection eligibility

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please will someone straighten out my confusion.
I've put comments into the code below to show where I need answers/direction:

 
author
Posts: 23907
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
Wow... there are lots of questions here... almost too many. Anyway, I guess I'll take one of the methods.




Henry
 
Henry Wong
author
Posts: 23907
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
Okay, I'll take another small method too...



Henry
 
Fantine Ponter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry, I'll try to drum that into my head. I guess I'll wait for the clarification of the main method before I can say I finally understand it all now...
 
Fantine Ponter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry, to answer your question:
***> BTW, what do you mean by "another useless reference"?

As I understand it, there seems to be no point in creating a reference if it has no object to reference, hence 'useless reference'.

I apologize for asking so many questions. The better I can understand each statement, and recognise the statements for what they are, the fewer questions I'll be asking in my very slow learning curve.

Thanks for your informative answers to my questions. I really appreciate it.
 
Henry Wong
author
Posts: 23907
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
Okay, some more code explained (from the main() method) ...

 
Fantine Ponter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay, now the pieces are starting to fall into place. Thank you!
 
Fantine Ponter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By George, I've got it! Thanks Henry
No more questions.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Fantine,

Years after you posted it, ;-)

Thank you for bringing the whole exercise onto coderanch, it is a good unraveling of the lesson in the book i have been doing.

And Henry, thank you for the explanation and teachings.

cheers,

Arend
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand this question doesn't everything pop of the stack eventually. What is the essence of the question.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Elim Banda wrote:I don't understand this question doesn't everything pop of the stack eventually. What is the essence of the question.



Yes, but objects exist on the heap.
The thing that pops off the stack is the reference, not the object itself.
It's the garbage collectors job to monitor whether an object on the heap has any references pointing at it.  If it doesn't then it is classed as eligible for garbage collection.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm really sorry for upping this old thread (I've registered here specifically to ask this question).
But I really struggle to understand the solution which is presented in the book to this Garbage Collection exercise.
The code is exactly the same as in topicstarter's post, and my initial understanding was that



Can somebody clear this out for me, please?
 
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

It's not very clear what your confusion is. My suggestion: Don't put your questions as comments in the code. The code listings have line numbers so reference the line numbers.

Something like this:

(your questions go outside of the code listing)

On line 1, why ... (whatever you're confused about) ...?

On line 3, what ... (whatever you're confused about) ...?
 
Alex Sheva
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:My suggestion: Don't put your questions as comments in the code. The code listings have line numbers so reference the line numbers.


I will try put it this way. Below is the clear code from the Head First Java Book.



My question is - on line 12, why gc1 acquires the value, if the method it refers to, is not being invoked on any object?
And because Henry Wong's answer above suggests that this is correct, it really confuses me.

Maybe it was supposed to be something like this there?

 
Junilu Lacar
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
doStuff() and doStuff2() are static methods of the GC class. Invoking static methods via a reference variable, which is what you're doing with gc2.doStuff(), is considered poor form even though it's syntactically legal. The proper way to invoke a static method is via the class name, like GC.doStuff() and GC.doStuff2().

As for why it's possible to invoke doStuff() without a class or object reference, the reason is due to scope and visibility rules. The call to doStuff() is made from within the same class, therefore, the reference part of the invocation is optional and implied to be the enclosing class, "GC.". It's the same reason using "this." is optional when invoking an instance method from another method of the same class or class hierarchy.
 
Alex Sheva
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Junilu Lacar, thank you, this really helps!
The "static" behavior of method was not cleared yet in the book, so I did not know that feature. I thought I was missing something that was already covered in the book.
 
Beware the other head of science - it bites! Nibble on this message:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic