how many objects r eligible for garbage Collection ?

Discussion in 'Java' started by Naveen Kumar, Feb 1, 2008.

  1. Naveen Kumar

    Naveen Kumar Guest

    I am confused by someone over object creation in java.. Here is a
    sample question..

    class Card {
    Short story=5;
    Card go(Card c)
    {
    c=null;
    return c;
    }

    public static void main(String[] args)
    {
    Card c1=new Card();
    Card c2=new Card();
    Card c3=c1.go(c2);
    c1=null;
    //do stuff;
    }

    when //do stuff is reached How many Objects are eligible for garbage
    Collection ?

    Does c3 occupies memory and is eligible for garbage collection..
    When we say

    Card c3 = null;

    Isnt memory allocated to this reference for storing valid object.. and
    is now available for gc .. ???
    Wht happens in case when i say Card c3 = null ?? Isnt heap and stack
    space utilized ?
    Naveen Kumar, Feb 1, 2008
    #1
    1. Advertising

  2. Naveen Kumar

    Lew Guest

    Naveen Kumar wrote:
    > I am confused by someone over object creation in java [sic].. Here is a
    > sample question..


    It's spelled "Java".

    > class Card {


    Why not public?

    > Short story=5;


    Please follow the indentation conventions in order to make your code more
    readable.

    Why is 'story' not private?

    > Card go(Card c)
    > {
    > c=null;
    > return c;
    > }
    >
    > public static void main(String[] args)
    > {
    > Card c1=new Card();
    > Card c2=new Card();
    > Card c3=c1.go(c2);
    > c1=null;
    > //do stuff;
    > }
    >
    > when //do stuff is reached How many Objects are eligible for garbage
    > Collection ?


    Is this homework?

    Here's how to solve this one: Identify all the object references in the
    program at that point. (Is 'story' one of them?) See which objects /used to
    have/ references but no longer do. Those are the ones eligible for GC.

    It might help to write down for each line of code which objects exist, and
    which ones from previous lines no longer have references to them.

    Follow the whole chain - if an object holds a reference to another object,
    there's another object to track.

    > Does c3 occupies memory and is eligible for garbage collection..


    What is the value of c3?

    Hint: null is not an object.

    > When we say
    >
    > Card c3 = null;
    >
    > Isnt memory allocated to this reference for storing valid object.. and
    > is now available for gc .. ???


    Reference to what? c3 is null, so it doesn't point to any object, valid or not.

    > Wht [sic] happens in case when i [sic] say Card c3 = null ?? Isnt heap and stack
    > space utilized ?


    What do you mean by "utilized"?

    What happens is that the variable 'c3' is cleared of any reference to any object.

    --
    Lew
    You only need /one/ punctuation mark at the end of each sentence!!!...!!!...!!!???
    The word "I" in English is capitalized.
    Lew, Feb 1, 2008
    #2
    1. Advertising

  3. Naveen Kumar

    Naveen Kumar Guest

    On Feb 1, 6:58 pm, Lew <> wrote:
    > Naveen Kumar wrote:
    > > I am confused by someone over object creation in java [sic].. Here is a
    > > sample question..

    >
    > It's spelled "Java".


    Well Lew, Are you programmer or high school grammar teacher [sick].

    >
    > > class Card {

    >
    > Why not public?
    >


    Why not default ? Does it cost you if I dont use public ?


    > > Short story=5;

    >
    > Please follow the indentation conventions in order to make your code more
    > readable.
    >


    Make sense to certain extent, but this code was for example and it
    cannot be used in eclipse to get the expected answer.

    > Why is 'story' not private?


    By not using 'private' is your privacy lost ? Cant Java variable be
    declared without being private ?

    >
    >
    >
    > > Card go(Card c)
    > > {
    > > c=null;
    > > return c;
    > > }

    >
    > > public static void main(String[] args)
    > > {
    > > Card c1=new Card();
    > > Card c2=new Card();
    > > Card c3=c1.go(c2);
    > > c1=null;
    > > //do stuff;
    > > }

    >
    > > when //do stuff is reached How many Objects are eligible for garbage
    > > Collection ?

    >
    > Is this homework?


    You never had any doubts in your life ? You dont have to sit in high
    school to get your doubts cleared.

    >
    > Here's how to solve this one: Identify all the object references in the
    > program at that point. (Is 'story' one of them?) See which objects /used to
    > have/ references but no longer do. Those are the ones eligible for GC.
    >
    > It might help to write down for each line of code which objects exist, and
    > which ones from previous lines no longer have references to them.
    >
    > Follow the whole chain - if an object holds a reference to another object,
    > there's another object to track.
    >
    > > Does c3 occupies memory and is eligible for garbage collection..

    >
    > What is the value of c3?
    >
    > Hint: null is not an object.


    Yes, but the confusion began because of it. What in case if i
    do not initialize the object as null. Have you ever tried using an
    variable without initializing it ? Like

    Card c3;
    This will throw error where as

    Card c3 = null;

    will never throw the error. Something happens in background. What
    exactly happens which make this work and not the previous one ?

    >
    > > When we say

    >
    > > Card c3 = null;

    >
    > > Isnt memory allocated to this reference for storing valid object.. and
    > > is now available for gc .. ???

    >
    > Reference to what? c3 is null, so it doesn't point to any object, valid or not.


    What exactly do you call c3 now if not object ? If c3 is not an
    object, what is it ?

    >
    > > Wht [sic] happens in case when i [sic] say Card c3 = null ?? Isnt heap and stack
    > > space utilized ?

    >
    > What do you mean by "utilized"?


    If you do not understand the word "utilized", guess you can sit and
    recollect your class 2 course.

    >
    > What happens is that the variable 'c3' is cleared of any reference to any object.


    Well, everyone knows about that. But the question was something else.

    >
    > --
    > Lew
    > You only need /one/ punctuation mark at the end of each sentence!!!...!!!...!!!???
    > The word "I" in English is capitalized.


    Thanks but no thanks for your grammatical correction. You were
    expected to answer technical stuff rather than grammar corrections.
    Answer the technical question if you can, else you can continue with
    your work.
    Naveen Kumar, Feb 1, 2008
    #3
  4. Naveen Kumar

    Naveen Kumar Guest

    Anyways, I got the answer. For others, there is just one object for
    gc, which is c1. c3 is just an null reference.

    Cheers
    Naveen Kumar

    On Feb 1, 11:48 pm, Naveen Kumar <> wrote:
    > On Feb 1, 6:58 pm, Lew <> wrote:
    >
    > > Naveen Kumar wrote:
    > > > I am confused by someone over object creation in java [sic].. Here is a
    > > > sample question..

    >
    > > It's spelled "Java".

    >
    > Well Lew, Are you programmer or high school grammar teacher [sick].
    >
    >
    >
    > > > class Card {

    >
    > > Why not public?

    >
    > Why not default ? Does it cost you if I dont use public ?
    >
    > > > Short story=5;

    >
    > > Please follow the indentation conventions in order to make your code more
    > > readable.

    >
    > Make sense to certain extent, but this code was for example and it
    > cannot be used in eclipse to get the expected answer.
    >
    > > Why is 'story' not private?

    >
    > By not using 'private' is your privacy lost ? Cant Java variable be
    > declared without being private ?
    >
    >
    >
    >
    >
    > > > Card go(Card c)
    > > > {
    > > > c=null;
    > > > return c;
    > > > }

    >
    > > > public static void main(String[] args)
    > > > {
    > > > Card c1=new Card();
    > > > Card c2=new Card();
    > > > Card c3=c1.go(c2);
    > > > c1=null;
    > > > //do stuff;
    > > > }

    >
    > > > when //do stuff is reached How many Objects are eligible for garbage
    > > > Collection ?

    >
    > > Is this homework?

    >
    > You never had any doubts in your life ? You dont have to sit in high
    > school to get your doubts cleared.
    >
    >
    >
    >
    >
    > > Here's how to solve this one: Identify all the object references in the
    > > program at that point. (Is 'story' one of them?) See which objects /used to
    > > have/ references but no longer do. Those are the ones eligible for GC.

    >
    > > It might help to write down for each line of code which objects exist, and
    > > which ones from previous lines no longer have references to them.

    >
    > > Follow the whole chain - if an object holds a reference to another object,
    > > there's another object to track.

    >
    > > > Does c3 occupies memory and is eligible for garbage collection..

    >
    > > What is the value of c3?

    >
    > > Hint: null is not an object.

    >
    > Yes, but the confusion began because of it. What in case if i
    > do not initialize the object as null. Have you ever tried using an
    > variable without initializing it ? Like
    >
    > Card c3;
    > This will throw error where as
    >
    > Card c3 = null;
    >
    > will never throw the error. Something happens in background. What
    > exactly happens which make this work and not the previous one ?
    >
    >
    >
    > > > When we say

    >
    > > > Card c3 = null;

    >
    > > > Isnt memory allocated to this reference for storing valid object.. and
    > > > is now available for gc .. ???

    >
    > > Reference to what? c3 is null, so it doesn't point to any object, valid or not.

    >
    > What exactly do you call c3 now if not object ? If c3 is not an
    > object, what is it ?
    >
    >
    >
    > > > Wht [sic] happens in case when i [sic] say Card c3 = null ?? Isnt heap and stack
    > > > space utilized ?

    >
    > > What do you mean by "utilized"?

    >
    > If you do not understand the word "utilized", guess you can sit and
    > recollect your class 2 course.
    >
    >
    >
    > > What happens is that the variable 'c3' is cleared of any reference to any object.

    >
    > Well, everyone knows about that. But the question was something else.
    >
    >
    >
    > > --
    > > Lew
    > > You only need /one/ punctuation mark at the end of each sentence!!!...!!!...!!!???
    > > The word "I" in English is capitalized.

    >
    > Thanks but no thanks for your grammatical correction. You were
    > expected to answer technical stuff rather than grammar corrections.
    > Answer the technical question if you can, else you can continue with
    > your work.
    Naveen Kumar, Feb 1, 2008
    #4
  5. Naveen Kumar <> writes:

    [regarding this program:]
    |
    | Card go(Card c)
    | {
    | c=null;
    | return c;
    | }
    |
    | public static void main(String[] args)
    | {
    | Card c1=new Card();
    | Card c2=new Card();
    | Card c3=c1.go(c2);
    | c1=null;
    | //do stuff;
    | }

    ....

    > Yes, but the confusion began because of it. What in case if i
    > do not initialize the object as null. Have you ever tried using an
    > variable without initializing it ? Like
    >
    > Card c3;
    > This will throw error where as


    It will fail to compile.

    > Card c3 = null;


    > will never throw the error. Something happens in background. What
    > exactly happens which make this work and not the previous one ?


    Local variable may not be *read* before they are initialized (i.e., a
    value is assigned to them). The language specification enforces this
    by requireing that allpaths of execution leading to a variable being
    read must include an assignment to the variable, and the compiler
    checks this requirement. Nothing happens in the background.

    > What exactly do you call c3 now if not object ? If c3 is not an
    > object, what is it ?


    c3 is a variable.
    It holds a value. Or rather, the location in the runtime environment
    that corresponds to the variable, holds a value.
    The type of that value must be a reference to a Card object.
    The value itself is null, which is a valid value of that type.

    No objects are referenced by c3's value.

    >> What do you mean by "utilized"?


    > If you do not understand the word "utilized", guess you can sit and
    > recollect your class 2 course.


    What do *you* mean by "utilizied" in *this* case? I.e., your question
    was not clear.

    The variable c3 is probably mapped to a stack location in the runtime
    environment. Assigning the null value to it will store the corresponding
    value in that stack location.

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Feb 1, 2008
    #5
  6. Naveen Kumar <> writes:

    > Anyways, I got the answer. For others, there is just one object for
    > gc, which is c1. c3 is just an null reference.


    Or more pedantically: The only object eligable for garbage collection
    is the one originally assigned to the variable c1. When trying to
    understand garbage collection, it's important to distinguish between
    objects, references to objects and variables holding references.

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Feb 1, 2008
    #6
  7. Naveen Kumar

    Lew Guest

    Lasse Reichstein Nielsen wrote:
    > Naveen Kumar <> writes:
    >
    >> Anyways, I got the answer. For others, there is just one object for
    >> gc, which is c1. c3 is just an null reference.

    >
    > Or more pedantically: The only object eligable for garbage collection
    > is the one originally assigned to the variable c1. When trying to
    > understand garbage collection, it's important to distinguish between
    > objects, references to objects and variables holding references.


    That's not true. You missed one other.

    --
    Lew
    Lew, Feb 2, 2008
    #7
  8. Naveen Kumar

    Lew Guest

    Naveen Kumar wrote:
    >> Naveen Kumar wrote:
    >>> class Card {


    Lew wrote:
    >> Why not public?
    >>

    >
    > Why not default ? Does it cost you if I dont use public ?


    Do you have some trouble answering other people's questions, or do you regard
    Usenet as your personal private help desk?

    I asked so I could understand, and perhaps learn from your reasoning. Aren't
    you willing to share your insights?

    >>> Short story=5;

    >> Please follow the indentation conventions in order to make your code more
    >> readable.


    > Make sense to certain extent, but this code was for example and it
    > cannot be used in eclipse to get the expected answer.


    Huh? What does that have to do with indenting so that the people you're
    asking for help have an easier time providing it?

    >> Why is 'story' not private?

    >
    > By not using 'private' is your privacy lost ? Cant Java variable be
    > declared without being private ?


    Boy, you sure are unwilling to participate in discussion, aren't you?

    Why are you so averse to providing your insights and reasoning?

    >>> Card go(Card c)
    >>> {
    >>> c=null;
    >>> return c;
    >>> }
    >>> public static void main(String[] args)
    >>> {
    >>> Card c1=new Card();
    >>> Card c2=new Card();
    >>> Card c3=c1.go(c2);
    >>> c1=null;
    >>> //do stuff;
    >>> }
    >>> when //do stuff is reached How many Objects are eligible for garbage
    >>> Collection ?


    >> Is this homework?


    > You never had any doubts in your life ? You dont have to sit in high
    > school to get your doubts cleared.


    So that's a "yes"?

    >> Here's how to solve this one: Identify all the object references in the
    >> program at that point. (Is 'story' one of them?) See which objects /used to
    >> have/ references but no longer do. Those are the ones eligible for GC.
    >>
    >> It might help to write down for each line of code which objects exist, and
    >> which ones from previous lines no longer have references to them.
    >>
    >> Follow the whole chain - if an object holds a reference to another object,
    >> there's another object to track.
    >>
    >>> Does c3 occupies memory and is eligible for garbage collection..

    >> What is the value of c3?
    >>
    >> Hint: null is not an object.

    >
    > Yes, but the confusion began because of it. What in case if i
    > do not initialize the object as null. Have you ever tried using an
    > variable without initializing it ? Like
    >
    > Card c3;
    > This will throw error where as
    >
    > Card c3 = null;
    >
    > will never throw the error. Something happens in background. What
    > exactly happens which make this work and not the previous one ?


    This is explained in the Java Language Specification (JLS).

    The variable 'c3' must be assigned either an object reference or null.

    >>> When we say
    >>> Card c3 = null;
    >>> Isnt memory allocated to this reference for storing valid object.. and
    >>> is now available for gc .. ???


    >> Reference to what? c3 is null, so it doesn't point to any object, valid or not.


    > What exactly do you call c3 now if not object ? If c3 is not an
    > object, what is it ?


    It's a variable. Variables and objects are different things.

    >>> Wht [sic] happens in case when i [sic] say Card c3 = null ?? Isnt heap and stack
    >>> space utilized ?

    >> What do you mean by "utilized"?

    >
    > If you do not understand the word "utilized", guess you can sit and
    > recollect your class 2 course.


    Boy, /that/ was rude. I was trying to understand your question, but you come
    back with snide hostility. O-kaaay.

    >> What happens is that the variable 'c3' is cleared of any reference to any object.

    >
    > Well, everyone knows about that. But the question was something else.


    Funny, you were asking what happens, so I told you. It wasn't clear from your
    question that you already knew the answer.

    > Thanks but no thanks for your grammatical correction. You were
    > expected to answer technical stuff rather than grammar corrections.
    > Answer the technical question if you can, else you can continue with
    > your work.


    My, my, we're bossy today! I am just astounded by how rude you are.

    It is certainly not anyone's job on Usenet to answer your questions, nor even
    to acknowlege them. This is a *discussion* group, not a help desk. You are
    not winning friends and influencing people with your high-handed attitude,
    Dale Carnegie.

    Why don't you lay off the arrogance and participate in the discussions like a
    participant, hm?

    --
    Lew
    Lew, Feb 2, 2008
    #8
  9. Naveen Kumar

    Naveen Kumar Guest

    On Feb 2, 5:18 am, Lew <> wrote:
    > Lasse Reichstein Nielsen wrote:
    > > Naveen Kumar <> writes:

    >
    > >> Anyways, I got the answer. For others, there is just one object for
    > >> gc, which is c1. c3 is just an null reference.

    >
    > > Or more pedantically: The only object eligable for garbage collection
    > > is the one originally assigned to the variable c1. When trying to
    > > understand garbage collection, it's important to distinguish between
    > > objects, references to objects and variables holding references.

    >
    > That's not true. You missed one other.
    >
    > --
    > Lew


    Which is the other one ?
    Naveen Kumar, Feb 2, 2008
    #9
  10. Naveen Kumar

    Lew Guest

    Naveen Kumar wrote:
    > On Feb 2, 5:18 am, Lew <> wrote:
    >> Lasse Reichstein Nielsen wrote:
    >>> Naveen Kumar <> writes:
    >>>> Anyways, I got the answer. For others, there is just one object for
    >>>> gc, which is c1. c3 is just an null reference.
    >>> Or more pedantically: The only object eligable for garbage collection
    >>> is the one originally assigned to the variable c1. When trying to
    >>> understand garbage collection, it's important to distinguish between
    >>> objects, references to objects and variables holding references.

    >> That's not true. You missed one other.
    >>
    >> --
    >> Lew

    >
    > Which is the other one ?


    The Short which is referenced within the Card.

    --
    Lew
    Lew, Feb 2, 2008
    #10
  11. Naveen Kumar

    Tim Smith Guest

    In article <>,
    Lew <> wrote:
    > > class Card {

    >
    > Why not public?


    Does it matter?

    > > Short story=5;

    >
    > Please follow the indentation conventions in order to make your code more
    > readable.
    >
    > Why is 'story' not private?


    Does it matter?

    ....
    > > Does c3 occupies memory and is eligible for garbage collection..

    >
    > What is the value of c3?
    >
    > Hint: null is not an object.
    >
    > > When we say
    > >
    > > Card c3 = null;
    > >
    > > Isnt memory allocated to this reference for storing valid object.. and
    > > is now available for gc .. ???

    >
    > Reference to what? c3 is null, so it doesn't point to any object, valid or
    > not.


    But it takes memory to hold that null. The original poster is a bit
    unclear on how memory works in Java (else he wouldn't be asking these
    questions), so quite possibly he wants to know if that memory is
    eligible for garbage collection.

    --
    --Tim Smith
    Tim Smith, Feb 2, 2008
    #11
  12. Naveen Kumar wrote:
    > I am confused by someone over object creation in java.. Here is a
    > sample question..
    >
    > class Card {
    > Short story=5;
    > Card go(Card c)
    > {
    > c=null;
    > return c;
    > }
    >
    > public static void main(String[] args)
    > {
    > Card c1=new Card();
    > Card c2=new Card();
    > Card c3=c1.go(c2);
    > c1=null;
    > //do stuff;
    > }
    >
    > when //do stuff is reached How many Objects are eligible for garbage
    > Collection ?


    Unless c2 is used in 'do stuff' then all the objects are eligible for
    collection. Merely having a local reference to an object is not
    sufficient to keep the object. Local references are deemed not to exist
    beyond their last point of use. This is not just theoretical either, the
    current Java 6 from Sun does this.

    Mark Thornton
    Mark Thornton, Feb 2, 2008
    #12
  13. Lew <> wrote:
    >>>> class Card {
    >>> Why not public?

    >> Why not default ? Does it cost you if I dont use public ?

    > I asked so I could understand, and perhaps learn from your reasoning. Aren't
    > you willing to share your insights?


    I'm not really willing to comment about homework or not, or
    about the asker's behaviour, but this particular point seemed
    quite obvious to me. It was a simple code-sample (almost an
    SSCCE), and anything else than default access would have added
    unnecessary (in this context) keywords :)
    Andreas Leitgeb, Feb 2, 2008
    #13
  14. Lew wrote:
    > Naveen Kumar wrote:
    >> On Feb 2, 5:18 am, Lew <> wrote:
    >>> Lasse Reichstein Nielsen wrote:
    >>>> Naveen Kumar <> writes:
    >>>>> Anyways, I got the answer. For others, there is just one object for
    >>>>> gc, which is c1. c3 is just an null reference.
    >>>> Or more pedantically: The only object eligable for garbage collection
    >>>> is the one originally assigned to the variable c1. When trying to
    >>>> understand garbage collection, it's important to distinguish between
    >>>> objects, references to objects and variables holding references.
    >>> That's not true. You missed one other.
    >>>
    >>> --
    >>> Lew

    >>
    >> Which is the other one ?

    >
    > The Short which is referenced within the Card.


    The Card object references by c2 might be available too, depending on
    what "// do stuff" does with it. Many (all?) GC implementations seem to
    not collect objects referenced by variables still on the stack, but I
    don't think they are forced to.

    //Roger Lindsjö
    Roger Lindsjö, Feb 2, 2008
    #14
  15. Mark Thornton wrote:
    > Naveen Kumar wrote:
    >> I am confused by someone over object creation in java.. Here is a
    >> sample question..
    >>
    >> class Card {
    >> Short story=5;
    >> Card go(Card c)
    >> {
    >> c=null;
    >> return c;
    >> }
    >>
    >> public static void main(String[] args)
    >> {
    >> Card c1=new Card();
    >> Card c2=new Card();
    >> Card c3=c1.go(c2);
    >> c1=null;
    >> //do stuff;
    >> }
    >>
    >> when //do stuff is reached How many Objects are eligible for garbage
    >> Collection ?

    >
    > Unless c2 is used in 'do stuff' then all the objects are eligible for
    > collection. Merely having a local reference to an object is not
    > sufficient to keep the object. Local references are deemed not to exist
    > beyond their last point of use. This is not just theoretical either, the
    > current Java 6 from Sun does this.

    ....

    More generally, I think this thread has failed to fully distinguish two
    concepts:

    1. Is an object unreachable, and therefore eligible for finalization?
    The test here is whether the object can be accessed by any continuing
    computation in any active thread. This is a property of the program and
    the language specification, regardless of JVM implementation.

    2. Will a particular JVM GC implementation, when dealing with a memory
    shortage, notice that, and collect it. This is subject to change as JVMs
    become more sophisticated.

    Unreachability is not, in general, computable. The JVM has to
    approximate it. Given the disastrous consequences of collecting a
    reachable object, it has to err in the direction of not collecting some
    unreachable objects. There will never be a perfect garbage collector
    that collects every unreachable object and no others.

    Patricia
    Patricia Shanahan, Feb 2, 2008
    #15
  16. Roger Lindsjö wrote:
    >
    > The Card object references by c2 might be available too, depending on
    > what "// do stuff" does with it. Many (all?) GC implementations seem to
    > not collect objects referenced by variables still on the stack, but I
    > don't think they are forced to.
    >


    Java 6 does collect objects where the only references have no further
    use. It is possible to observe this behaviour if you also have a weak
    reference to the 'garbage'.

    Mark Thornton
    Mark Thornton, Feb 2, 2008
    #16
  17. Naveen Kumar

    Lew Guest

    Mark Thornton wrote:
    > Roger Lindsjö wrote:
    >>
    >> The Card object references by c2 might be available too, depending on
    >> what "// do stuff" does with it. Many (all?) GC implementations seem
    >> to not collect objects referenced by variables still on the stack, but
    >> I don't think they are forced to.
    >>

    >
    > Java 6 does collect objects where the only references have no further
    > use. It is possible to observe this behaviour if you also have a weak
    > reference to the 'garbage'.


    This impinges on another thread discussing variables that are not unstacked
    after their block until the end of their method. Someone proposed that
    nulling such a variable would "help" GC; the observation here is that such
    "help" is completely unnecessary.

    --
    Lew
    Lew, Feb 2, 2008
    #17
  18. Naveen Kumar

    Lew Guest

    Andreas Leitgeb wrote:
    > Lew <> wrote:
    >>>>> class Card {
    >>>> Why not public?
    >>> Why not default ? Does it cost you if I dont use public ?

    >> I asked so I could understand, and perhaps learn from your reasoning. Aren't
    >> you willing to share your insights?

    >
    > I'm not really willing to comment about homework or not, or
    > about the asker's behaviour, but this particular point seemed
    > quite obvious to me. It was a simple code-sample (almost an
    > SSCCE), and anything else than default access would have added
    > unnecessary (in this context) keywords :)


    I was wondering if that was a reason, or if there were others I hadn't thought
    of. It's always edifying to find out why people do things, especially if they
    are unexpected things.

    One could argue that leaving off the expected "public" keyword actually
    diminishes the pedagogical value of an example, as the unusual idiom actually
    distracts one from whatever the primary purpose of the example is.

    Inquiring minds simply want to know. Andreas's guess is plausible.

    --
    Lew
    Lew, Feb 2, 2008
    #18
  19. Patricia Shanahan wrote:
    > More generally, I think this thread has failed to fully distinguish two
    > concepts:
    >
    > 1. Is an object unreachable, and therefore eligible for finalization?
    > The test here is whether the object can be accessed by any continuing
    > computation in any active thread. This is a property of the program and
    > the language specification, regardless of JVM implementation.
    >


    E.g. a reference that would only be used if System.currentTimeMillis()
    returned a negative number might be considered 'dead', but I doubt we
    will ever see a JVM that recognises that.

    Mark Thornton
    Mark Thornton, Feb 2, 2008
    #19
  20. Naveen Kumar

    Lew Guest

    Tim Smith wrote:
    > In article <>,
    > Lew <> wrote:
    >>> class Card {

    >> Why not public?

    >
    > Does it matter?


    Yes.

    >>> Short story=5;

    >> Please follow the indentation conventions in order to make your code more
    >> readable.
    >>
    >> Why is 'story' not private?

    >
    > Does it matter?


    Yes.

    > ....
    >>> Does c3 occupies memory and is eligible for garbage collection..

    >> What is the value of c3?
    >>
    >> Hint: null is not an object.
    >>
    >>> When we say
    >>>
    >>> Card c3 = null;
    >>>
    >>> Isnt memory allocated to this reference for storing valid object.. and
    >>> is now available for gc .. ???

    >> Reference to what? c3 is null, so it doesn't point to any object, valid or
    >> not.

    >
    > But it takes memory to hold that null. The original poster is a bit
    > unclear on how memory works in Java (else he wouldn't be asking these
    > questions), so quite possibly he wants to know if that memory is
    > eligible for garbage collection.


    There is no such thing as GCing a null, so no, memory to hold a null reference
    is not eligible for GC.

    --
    Lew
    Lew, Feb 2, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jimjim
    Replies:
    0
    Views:
    365
    jimjim
    Feb 27, 2004
  2. Øyvind Isaksen
    Replies:
    1
    Views:
    965
    Øyvind Isaksen
    May 18, 2007
  3. Mark Space
    Replies:
    18
    Views:
    690
    Eric Sosman
    Jul 17, 2007
  4. Larry Epn
    Replies:
    8
    Views:
    3,353
    Allen Chen [MSFT]
    Mar 16, 2009
  5. Krzysztof Cierpisz
    Replies:
    0
    Views:
    525
    Krzysztof Cierpisz
    Aug 18, 2011
Loading...

Share This Page