473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

instance Question

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;
}

}
Jan 16 '08 #1
12 1361


"Analizer1" <dv*****@sbcglobal.netwrote in message
news:iS***************@nlpi061.nbdc.sbc.com...
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
--
Gianluca Gravina
http://blogs.ugidotnet.org/thinkingingrava

Jan 16 '08 #2
On Wed, 16 Jan 2008 10:20:41 -0800, Analizer1 <dv*****@sbcglobal.net
wrote:
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
Jan 16 '08 #3
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].
Jan 16 '08 #4
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?
"Analizer1" <dv*****@sbcglobal.netwrote in message
news:iS***************@nlpi061.nbdc.sbc.com...
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;
}

}


Jan 16 '08 #5
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.

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Analizer1" <dv*****@sbcglobal.netwrote in message
news:iS***************@nlpi061.nbdc.sbc.com...
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;
}

}


Jan 16 '08 #6
On Wed, 16 Jan 2008 10:55:41 -0800, Analizer1 <dv*****@sbcglobal.net>
wrote:
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
Jan 16 '08 #7
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

"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:uN**************@TK2MSFTNGP06.phx.gbl...
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.

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Analizer1" <dv*****@sbcglobal.netwrote in message
news:iS***************@nlpi061.nbdc.sbc.com...
>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;
}

}



Jan 16 '08 #8
On Wed, 16 Jan 2008 11:30:26 -0800, Analizer1 <dv*****@sbcglobal.net>
wrote:
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.
Jan 16 '08 #9
Hi,

"Analizer1" <dv*****@sbcglobal.netwrote in message
news:IT***************@nlpi061.nbdc.sbc.com...
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.
Or does the method occupy the same memory address , and all 3 instances
actually use the same Method (executable code)

tks

"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:uN**************@TK2MSFTNGP06.phx.gbl...
>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.

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Analizer1" <dv*****@sbcglobal.netwrote in message
news:iS***************@nlpi061.nbdc.sbc.com...
>>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;
}

}



--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
Jan 16 '08 #10

"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:OR**************@TK2MSFTNGP03.phx.gbl...
Hi,

"Analizer1" <dv*****@sbcglobal.netwrote in message
news:IT***************@nlpi061.nbdc.sbc.com...
>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.
>Or does the method occupy the same memory address , and all 3 instances
actually use the same Method (executable code)
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.

Jan 16 '08 #11
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).

--
Arto Viitanen
Jan 17 '08 #12
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.

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Scott Roberts" <sr******@no.spam.here-webworks-software.comwrote in
message news:%2****************@TK2MSFTNGP03.phx.gbl...
>
"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:OR**************@TK2MSFTNGP03.phx.gbl...
>Hi,

"Analizer1" <dv*****@sbcglobal.netwrote in message
news:IT***************@nlpi061.nbdc.sbc.com...
>>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.
>>Or does the method occupy the same memory address , and all 3 instances
actually use the same Method (executable code)

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.

Jan 17 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
0
by: cat | last post by:
HiFolks, I am newbie in DB2 ESE. Here is the question, I already created an instance db2inst1 under file system /db2home/db2inst1 ( actually the instance has been created after installation.)...
5
by: allison | last post by:
Can someone point me to a guide on when to use static methods versus instance methods in a class? For instance, is it bad design to have a static method that creates an instance of another class? ...
6
by: Dmitry Karneyev | last post by:
Hi! I guess this question have been asked a lot of times, but please be tolerant and if you have any ideas share it. The question is: how to make availibale only one instance of application and...
20
by: Shawnk | last post by:
I would like to get the class INSTANCE name (not type name) of an 'object'. I can get the object (l_obj_ref.GetType()) and then get the (l_obj_typ.Name) for the class name. I there any way of...
4
by: Tony Johansson | last post by:
Hello! I have a class definition called MyClass see below. I create an instance of this class MyClass I also want this instance to be able to modify the test instance that exist in this...
9
by: manstey | last post by:
Hi, My question probably reflects my misunderstanding of python objects, but I would still like to know the answer. The question is, is it possible for an instnace to have a value (say a...
5
by: Andy B | last post by:
I am trying to figure out how to make an object instance available for all methods of a class. I tried to do something like this: public class test { TheObject Instance = new TheObject();...
19
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I'm doing my c# more and more like i used to code c++, meaning i'm casting more often than creating an instance of objects. like : protected void gvOrderDetailsRowDataBound(object sender,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.