instance Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Analizer1

    #1

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

    }


  • grava

    #2
    Re: instance Question



    "Analizer1" <dvs_bis@sbcglo bal.netwrote in message
    news:iSrjj.171$ hI1.162@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


    Comment

    • Peter Duniho

      #3
      Re: instance Question

      On Wed, 16 Jan 2008 10:20:41 -0800, Analizer1 <dvs_bis@sbcglo bal.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

      Comment

      • JS

        #4
        Re: instance Question

        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].

        Comment

        • Analizer1

          #5
          Re: instance Question

          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" <dvs_bis@sbcglo bal.netwrote in message
          news:iSrjj.171$ hI1.162@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;
          }
          >
          }
          >
          >

          Comment

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

            #6
            Re: instance Question

            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
            The #1 Warehouse Management System & Direct Store Delivery Software (DSD) for QuickBooks & ERP Systems – LaceUp Solutions

            Mobile & warehouse Solutions.
            "Analizer1" <dvs_bis@sbcglo bal.netwrote in message
            news:iSrjj.171$ hI1.162@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;
            }
            >
            }
            >
            >

            Comment

            • Peter Duniho

              #7
              Re: instance Question

              On Wed, 16 Jan 2008 10:55:41 -0800, Analizer1 <dvs_bis@sbcglo bal.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

              Comment

              • Analizer1

                #8
                Re: instance Question

                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:uNCMwDHWIH A.5340@TK2MSFTN GP06.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
                The #1 Warehouse Management System & Direct Store Delivery Software (DSD) for QuickBooks & ERP Systems – LaceUp Solutions

                Mobile & warehouse Solutions.
                "Analizer1" <dvs_bis@sbcglo bal.netwrote in message
                news:iSrjj.171$ hI1.162@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;
                > }
                >>
                >}
                >>
                >>
                >
                >

                Comment

                • Peter Duniho

                  #9
                  Re: instance Question

                  On Wed, 16 Jan 2008 11:30:26 -0800, Analizer1 <dvs_bis@sbcglo bal.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.

                  Comment

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

                    #10
                    Re: instance Question

                    Hi,



                    "Analizer1" <dvs_bis@sbcglo bal.netwrote in message
                    news:ITsjj.179$ hI1.124@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:uNCMwDHWIH A.5340@TK2MSFTN GP06.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" <dvs_bis@sbcglo bal.netwrote in message
                    >news:iSrjj.171 $hI1.162@nlpi06 1.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
                    The #1 Warehouse Management System & Direct Store Delivery Software (DSD) for QuickBooks & ERP Systems – LaceUp Solutions

                    Mobile & warehouse Solutions.


                    Comment

                    • Scott Roberts

                      #11
                      Re: instance Question


                      "Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions .comwrote in
                      message news:OR7XKkIWIH A.4076@TK2MSFTN GP03.phx.gbl...
                      Hi,
                      >
                      >
                      >
                      "Analizer1" <dvs_bis@sbcglo bal.netwrote in message
                      news:ITsjj.179$ hI1.124@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 "dosomethin g()"
                      code you will have 3 copies of the "iRet" variable.

                      Comment

                      • Arto Viitanen

                        #12
                        Re: instance Question

                        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

                        Comment

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

                          #13
                          Re: instance Question

                          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
                          The #1 Warehouse Management System & Direct Store Delivery Software (DSD) for QuickBooks & ERP Systems – LaceUp Solutions

                          Mobile & warehouse Solutions.
                          "Scott Roberts" <sroberts@no.sp am.here-webworks-software.comwro te in
                          message news:%23Y1xIrIW IHA.4868@TK2MSF TNGP03.phx.gbl. ..
                          >
                          "Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions .comwrote in
                          message news:OR7XKkIWIH A.4076@TK2MSFTN GP03.phx.gbl...
                          >Hi,
                          >>
                          >>
                          >>
                          >"Analizer1" <dvs_bis@sbcglo bal.netwrote in message
                          >news:ITsjj.179 $hI1.124@nlpi06 1.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
                          "dosomethin g()" code you will have 3 copies of the "iRet" variable.

                          Comment

                          Working...