• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

object lifespan

Hi have a question about an objects lifespan.

I have a static method in a class that returns an object to a null object.

MyClass myclass;

myclass = ClassServices.GetClass(1);

What happens to the object that got created in the GetClass method? Does it get destoyed after being assigned to 'myclass'?




0
tmueller18
Asked:
tmueller18
  • 4
  • 3
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
No. Your method will return a reference to the object which will be stored in the variable myclass. The object will not be destroyed until sometime after the last reference to it goes out of scope. So in your case it won't be destroyed until at least the time that myclass goes out of scope. Unless of course you are returning a clone of the object rather than a simple reference in which case the original cloned object will drop out of scope at the end of the GetClass method (assuming it isn't a static object and isn't referenced from anywhere else).
0
 
tmueller18Author Commented:
the GetClass static method likes like this

public static PersonClass GetClass(int ID)
{
      PersonClass result= new  PersonClass();
      return result;
}

---------------------------
calling page:
----------------------------
PersonClass myPersonClass;
myPersonClass = PersonServices.GetClass(3);


what happens to 'result'?

From what you said the result object doesnt live outside the scope of the method. Is this correct?

0
 
Carl TawnSystems and Integration DeveloperCommented:
"result" isn't actually an object, it is a variable that holds a reference to an object.

"result" will drop out of scope at the end of the method, but the object you are returning will not because a reference to the object is still being held by myPersonClass outside of the GetClass method. The actual object being referenced will not drop out of scope until at least the time that the "myPersonClass" variable drops out of scope.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
tmueller18Author Commented:
"result" isn't actually an object, it is a variable that holds a reference to an object.

'result'  is an object since it was instantiated.

'result' is passed to the variable 'myPersonClass'

result drops out of scope now?



0
 
Carl TawnSystems and Integration DeveloperCommented:
"result" isn't an object, "result" simply points to an object.

Think of your object as a balloon; "result" is a piece of string through which you can get to the balloon, when you return from your method you are tying another piece of string (the "myPersonClass" variable) to the balloon. When the method exits the "result" piece of string is cut off and destroyed, the balloon stays exactly where it was and is still being referenced by the "myPersonClass" variable.

So, the "result" variable drops out of scope when the method exits, but the actual object you created still remains in memory and is now referenced by the "myPersonClass" variable.
0
 
tmueller18Author Commented:
so when i create an object like this:

Foo f = new Foo();

f isnt an object but a pointer to an object thats created in memory?

i understand the baloon part. thanks for explaing the scope of the variable

0
 
Carl TawnSystems and Integration DeveloperCommented:
Yes, basically.

f is a variable that holds a reference to an object of type Foo. then the "new Foo()" part creates an object in memory somewhere and stores the address for that block of memory in the variable f.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now