instance Question

G

grava

Analizer1 said:
Hi....
when i use the new keyword

myobject obj1 = new myobject()
myobject obj2 = new myobject()
myobject obj3 = new myobject()

does each of the object have a complete instance of
myobject, or do they share the methods of myobject
another words..does each instance have a complet object/methods/date
withing itself

tks


public class myobject
{
public myobject()
{
....do some stuff;
}
public int dosomthing()
{
int iRet=0;
....do some stuff;
return iRet;
}

}
With the code you've written each object have its complete
object/methos/date definitions. If you define something as "static" then the
static thing (data or methods) are shared ...

HTH
 
A

Analizer1

Hi....
when i use the new keyword

myobject obj1 = new myobject()
myobject obj2 = new myobject()
myobject obj3 = new myobject()

does each of the object have a complete instance of
myobject, or do they share the methods of myobject
another words..does each instance have a complet object/methods/date withing
itself

tks


public class myobject
{
public myobject()
{
....do some stuff;
}
public int dosomthing()
{
int iRet=0;
....do some stuff;
return iRet;
}

}
 
P

Peter Duniho

Hi....
when i use the new keyword

myobject obj1 = new myobject()
myobject obj2 = new myobject()
myobject obj3 = new myobject()

does each of the object have a complete instance of
myobject, or do they share the methods of myobject
another words..does each instance have a complet object/methods/date
withing itself

You get multiple objects, each with their own copies of the data members
(i.e fields...I assume that's what you meant by "date").

However, they don't have (nor do they need) their own copies of the
executable code (i.e. methods). Nor will they have their own copies of
any fields declared as "static.

Pete
 
J

JS

Each object will have its own copy of the fields from the class.

[The executable code for the class (e.g. methods and properties) is
not duplicated when constructing a new instance].
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I guest that your question is what is copied (and memory reserverd for it)
when a new instance is created.
If that is your question then the answer is only those members that can
change from instance to instance. a member variable is. A member const is
not (as it will be the same always) the Code for a method is not as it will
be the same for all the instances.

hope this answer your question.
 
A

Analizer1

So the above example...
if i was to run the 3 objects in there own threads.....
they would all use the same executable code (method)

is this correct?
 
P

Peter Duniho

So the above example...
if i was to run the 3 objects in there own threads.....
they would all use the same executable code (method)

is this correct?

Yes. Not that this would be a problem. As long as the _data_ on which
the code is operating is independent for each thread, the fact that each
object has its own thread would be immaterial with respect to the
execution of that code.

Of course, usually at _some_ point work done on one thread needs to be
communicated to some other thread. So eventually you're likely to have
some sort of thread synchronization to address. But as far as the object
itself is concerned, it is possible (and even typical) to have each
instance of the object operate completely independently of the other
objects.

Pete
 
A

Analizer1

so When the method is called 3 times from 3 different instances...
Is the method loaded into different memory locations , so each instance has
its own?
Or does the method occupy the same memory address , and all 3 instances
actually use the same Method (executable code)

tks
 
P

Peter Duniho

so When the method is called 3 times from 3 different instances..
Is the method loaded into different memory locations , so each instance
has
its own?
Or does the method occupy the same memory address , and all 3 instances
actually use the same Method (executable code)

The latter.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,



Analizer1 said:
so When the method is called 3 times from 3 different instances...
Is the method loaded into different memory locations , so each instance
has its own?

The method's code exist in only one place in memory.
 
S

Scott Roberts

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,





The method's code exist in only one place in memory.

Just want to clarify that the method's local variables are not stored with
the method's "code". So while you only have one copy of the "dosomething()"
code you will have 3 copies of the "iRet" variable.
 
A

Arto Viitanen

Analizer1 kirjoitti:
so When the method is called 3 times from 3 different instances...
Is the method loaded into different memory locations , so each instance has
its own?
Or does the method occupy the same memory address , and all 3 instances
actually use the same Method (executable code)

In C# kind of programs, the code is shared among instances of the class,
but the code is separate. With addition to fields called member fields,
there is "hidden" field named this. So when the code refers to field, like

aField = bField+3

inside the class, actually it does

this.aField = this.bField+3

and when the method starts, this is set to refer to the instance (for
example obj1), so the code actually means

obj1.aField = obj1.bField+3

So, same code can access several instances.

(Actually the first time you refer to the class's method the method ia
loaded into the .NET virtual machine, so you can think that when there
is no instances, there is no code, but when there is one or more
instances, there is one code).
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


The method variables are stored nowhere in memory, yes you read that right.
only when the method start executing it reserve a space enough to hold all
the variables in one part of the memory named Stack. When the method ends
that memory is recovered.
 

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

Top