can anyone explain stactic vs instance method by understanding example plz

Recommended Answers

All 4 Replies

static... doesn't have to be instantiated, lives in a static class, and can be called at any time by just the method name.

static class classA
{

public static void dosomething()
{
//method that does something
}

}

intance methods live in a class that must be referenced and constructed...

public class classB
{
   classB()
{
//constructor
}
public void dosomething()
{
 //method that does somthing.
}

}

an instance of the class that contains it would have to be created as an object and the method it contained would be referenced via the object variable you created.
ex.

classB clsb = new classB();
clsb.dosomething();

simple. google it next time.

Class members qualified with static modifier are called static members.

Class members declared without static modifier are called instance members.

dint understand what u are telling..can u explain m e in detail?

dint understand what u are telling..can u explain m e in detail?

Here is code of Test class.

using System;
  public class Test {
           public static void Print() {    // Static Method
                  Console.WriteLine("Hello - Static");
           }
          public void Show() {   // Instance Method
                 Console.WriteLine("Hello - Instance");
         }
  }

How to make a call to instance & static method?

public class MainTest{
        public static void Main() {
               // Static Method
               Test.Print();  

               Test a=new Test();  // Create an instance
               a.Show();                 
        }
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.