Implicit object creation

R

RHC

I have a question regarding garbage collection. Assume the following

public class Myclass
{
public Myclass()
{}
public int getTheNumberOne()
{ return 1;}

}

Now suppose in some other class, say a servlet or something I have the following..

int first = new Myclass().getTheNumberOne();

when / how is this new Object of type Myclass garbage collected???

TIA

RHC
 
S

Steve W. Jackson

:I have a question regarding garbage collection. Assume the following
:
:public class Myclass
:{
: public Myclass()
: {}
: public int getTheNumberOne()
: { return 1;}
:
:}
:
:Now suppose in some other class, say a servlet or something I have the
:following..
:
:int first = new Myclass().getTheNumberOne();
:
:when / how is this new Object of type Myclass garbage collected???
:
:TIA
:
:RHC

The how is the same as any other garbage collection.

The when is entirely at the discretion of the JVM based on numerous
factors. But it's eligible for collection immediately after the
execution of that line since you're not keeping a reference to the
created object.

= Steve =
 
H

Hal Rosser

RHC said:
I have a question regarding garbage collection. Assume the following

public class Myclass
{
public Myclass()
{}
public int getTheNumberOne()
{ return 1;}

}

Now suppose in some other class, say a servlet or something I have the following..

int first = new Myclass().getTheNumberOne();

when / how is this new Object of type Myclass garbage collected???

TIA

RHC

That's the good thing about it - you don't need to know - its taken care of
by the jvm
 
C

Chris Uppal

Hal said:
That's the good thing about it - you don't need to know - its taken care
of by the jvm

Agreed. But I think it's worth adding that most GC implementations are
specifically optimised to handle short-lived garbage (such as the temporary
object in the OP's post) efficiently.

-- chris
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top