Static vs. Non-Static Methods

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

    #1

    Static vs. Non-Static Methods

    Greetings,

    I have a "newbie" question relating to C#. I am still trying to
    understand the difference between a static and a non-static method
    (particularly when it is called), and was wondering if anyone could
    point me to some additional resources? If I see a method call, how can
    I be sure that it is Static (or Non-Static)?

    Thanks in advance!



    *** Sent via Developersdex http://www.developersdex.com ***
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Static vs. Non-Static Methods

    OutdoorGuy,

    A static method does not require an instance of an object to be
    executed. A general rule-of-thumb would be to make the method static if you
    do not rely on any of the fields that are specific to an instance of an
    object.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "OutdoorGuy " <OutdoorGuy@fis hing.com> wrote in message
    news:%23ngNqz2a FHA.740@tk2msft ngp13.phx.gbl.. .[color=blue]
    > Greetings,
    >
    > I have a "newbie" question relating to C#. I am still trying to
    > understand the difference between a static and a non-static method
    > (particularly when it is called), and was wondering if anyone could
    > point me to some additional resources? If I see a method call, how can
    > I be sure that it is Static (or Non-Static)?
    >
    > Thanks in advance!
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***[/color]


    Comment

    • OutdoorGuy

      #3
      Re: Static vs. Non-Static Methods

      Thanks, Nicholas. By that definition, then, the method below(i.e.,
      "GetAge()") would be considered a "Non-Static", "Private" method since
      an instance of the "Employee" object precedes the call. Am I correct?

      public static void Main()
      {
      Employee myEmployee = new Employee();
      int age = myEmployee.GetA ge();
      }

      Thanks.

      Sherwood

      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Static vs. Non-Static Methods

        OutdoorGuy,

        It would not be static, yes, but it might not be private. If Main is
        attached to another class in the same assembly, then GetAge could be
        internal or public. However, chances are it is public.

        Also, why have a method GetAge? Why not just have an Age property?
        This is cleaner, IMO.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "OutdoorGuy " <OutdoorGuy@fis hing.com> wrote in message
        news:%2338uAd3a FHA.3932@TK2MSF TNGP12.phx.gbl. ..[color=blue]
        > Thanks, Nicholas. By that definition, then, the method below(i.e.,
        > "GetAge()") would be considered a "Non-Static", "Private" method since
        > an instance of the "Employee" object precedes the call. Am I correct?
        >
        > public static void Main()
        > {
        > Employee myEmployee = new Employee();
        > int age = myEmployee.GetA ge();
        > }
        >
        > Thanks.
        >
        > Sherwood
        >
        > *** Sent via Developersdex http://www.developersdex.com ***[/color]


        Comment

        • Carlos J. Quintero [.NET MVP]

          #5
          Re: Static vs. Non-Static Methods

          Yes, in this case, since it is obvious that "age" is specific to each
          employee. An example of static method would be:

          Employee.GetAge Difference(Empl oyee1, Employee2)

          which also could be converted into a non-static (instance) method as

          Employee1.GetAg eDifference(Emp loyee2)

          --

          Best regards,

          Carlos J. Quintero

          MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
          You can code, design and document much faster.
          Free resources for add-in developers:
          MZ-Tools has a single goal: To make your everyday programming life easier. As an add-in to several Integrated Development Environment (IDEs) from Microsoft, MZ-Tools adds new menus and toolbars to them that provide many new productivity features.



          "OutdoorGuy " <OutdoorGuy@fis hing.com> escribió en el mensaje
          news:%2338uAd3a FHA.3932@TK2MSF TNGP12.phx.gbl. ..[color=blue]
          > Thanks, Nicholas. By that definition, then, the method below(i.e.,
          > "GetAge()") would be considered a "Non-Static", "Private" method since
          > an instance of the "Employee" object precedes the call. Am I correct?
          >
          > public static void Main()
          > {
          > Employee myEmployee = new Employee();
          > int age = myEmployee.GetA ge();
          > }
          >
          > Thanks.
          >
          > Sherwood
          >[/color]

          Comment

          • Cool Guy

            #6
            Re: Static vs. Non-Static Methods

            OutdoorGuy <OutdoorGuy@fis hing.com> wrote:
            [color=blue]
            > By that definition, then, the method below(i.e., "GetAge()") would be
            > considered a "Non-Static", "Private" method since an instance of the
            > "Employee" object precedes the call. Am I correct?
            >
            > public static void Main()
            > {
            > Employee myEmployee = new Employee();
            > int age = myEmployee.GetA ge();
            > }[/color]

            It's a non-static method (in other words, an instance method), yes.

            However, it's not private. If it were private then Main wouldn't be able
            to call it, since it would be invisible outside of Employee.[*]
            [*] This is ignoring a certain scenario involving nested classes in which
            private members of one object can be accessed by a different object, but
            that's slightly more advanced.

            Comment

            • Frisky

              #7
              Re: Static vs. Non-Static Methods

              Static methods belong to the class. Non-static methods belong to an instance
              of a class. So, to call a non-static method, you must create an instance of
              the class first. For example,

              public class Foo {
              public Foo() {}
              public void InstanceMethod( ) {}
              public static void ClassMethod() {}
              }

              To call an instance method, I first need an instance. I create an instance
              of a class by creating one with new. Then I can call its method. For
              example:

              Foo myFoo = new Foo() // create an instance
              myFoo.InstanceM ethod();

              To call a class method, I do not create an instance. I just specify the
              class and I can use it. It is globaly available through the class name. For
              example:

              Foo.ClassMethod ();

              Note: This brings up an interesting point. Static methods may not access
              data in the class except for static data. Since there is no instance of a
              class, the data would not exist.

              Statics are used a lot for utility classes where you have serveral related
              functions. The Math class in .Net is a good example. Another use is for
              Factory methods. See the Fatory Method design pattern.

              Hope this helps....

              Frisky

              "OutdoorGuy " <OutdoorGuy@fis hing.com> wrote in message
              news:%23ngNqz2a FHA.740@tk2msft ngp13.phx.gbl.. .[color=blue]
              > Greetings,
              >
              > I have a "newbie" question relating to C#. I am still trying to
              > understand the difference between a static and a non-static method
              > (particularly when it is called), and was wondering if anyone could
              > point me to some additional resources? If I see a method call, how can
              > I be sure that it is Static (or Non-Static)?
              >
              > Thanks in advance!
              >
              >
              >
              > *** Sent via Developersdex http://www.developersdex.com ***[/color]


              Comment

              • Michael S

                #8
                Re: Static vs. Non-Static Methods


                "Frisky" <Frisky_NOSPAM@ NorthPole.net> wrote in message
                news:ucf4bA%23a FHA.2288@TK2MSF TNGP14.phx.gbl. ..[color=blue]
                > See the Fatory Method design pattern.[/color]

                I think I get that the Fatory design pattern sports a .Bloat() method, but
                does it also encompass a Memento pattern to handle versioning? Like a
                ..GoOnDiet() method?

                Sorry.. just couldn't help my self. My Bad. =)

                - Michael S

                ps. Well explained Frisky.


                Comment

                • Frisky

                  #9
                  Re: Static vs. Non-Static Methods

                  Are you saying I'm fat? :)

                  --
                  Frisky

                  Intellectuals solve problems; geniuses prevent them. ~ Albert Einstein
                  "Michael S" <a@b.c> wrote in message
                  news:eaq%23X7Ab FHA.2768@tk2msf tngp13.phx.gbl. ..[color=blue]
                  >
                  > "Frisky" <Frisky_NOSPAM@ NorthPole.net> wrote in message
                  > news:ucf4bA%23a FHA.2288@TK2MSF TNGP14.phx.gbl. ..[color=green]
                  >> See the Fatory Method design pattern.[/color]
                  >
                  > I think I get that the Fatory design pattern sports a .Bloat() method, but
                  > does it also encompass a Memento pattern to handle versioning? Like a
                  > .GoOnDiet() method?
                  >
                  > Sorry.. just couldn't help my self. My Bad. =)
                  >
                  > - Michael S
                  >
                  > ps. Well explained Frisky.
                  >[/color]


                  Comment

                  Working...