• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

garbage collection and final variables

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have such code (in our class):

Value is defined class.

Does garbage collector clear it? We can't change it into null value (it is constant) and garbage collector clears up only references which are null-values. Is it possible to let garbage collector clear it?
[ July 18, 2007: Message edited by: Piotr Milewski ]
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Piotr Milewski:
Does garbage collector clear it?

Yes, since final only makes the reference variable read only, and has no further effect on the object.
 
Piotr Milewski
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand.

When such class is cleared? An reference can't be null (then non-final (references) classes are cleared by garbage collector), so how does GC knows when to clear it?
[ July 18, 2007: Message edited by: Piotr Milewski ]
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two possiblities: reference variable is a local variable (aka, authomatic or method variable), or it is a member of a class.

In the first case:

In this case, the object referenced by variable 'a' is ellgible for garbage collection when myMethod() returns (i.e. when the reference variable goes out of scope).

In the second case:

In this case the object referenced by class' instance variable 'a' is ellegible for garbage collection when the object it belongs to (e.g. the object referenced by obj) becomes ellegible for garbage collection.

I hope it is clear now.
[ July 18, 2007: Message edited by: Sergio Tridente ]
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Piotr,

I think you have a misunderstanding about the GC and objects and their reference variables - please think about your original post and see if you can find the flaw in your assumptions
 
Piotr Milewski
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sergio. Yes, I meant the second case. Thank you for such complete explanation - it really helped me .

Thanks Manfred and Bert - you're right - I misunderstood some basic principles (like I was blind), forgot about it, but never more
 
Don't listen to Steve. Just read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic