The difference between static and non-static
static:
the static keyword
using the class name to call
in a static method can access static members
in a static method, can not directly access instance members
before calling needs initialization (constructor)
non-static:
no need to use the static keyword
use cases call the object
in an instance method can directly access static members
can access instance members directly in an instance method
needs to initialize an instance of an object (constructor)
for static class, in class only declares a static member, Instead instance of the class can be declared static class members.
. 1 class Program { 2 public static void the Main ( String [] args) { . 3 Person.SayHello (); . 4 Animal Animal = new new Animal (); . 5 the Console.ReadKey (); . 6 } . 7 } . 8 static the Person { . 9 static the Person () { 10 Console.WriteLine ( " I is a static class constructor " ); . 11 } 12 is public static void the SayHello () { 13 is Console.WriteLine ( " I is a static method " ); 14 } 15 } 16 class Animal { . 17 public Animal () { 18 is Console.WriteLine ( " I instantiated class constructor " ); 19 } 20 }
In the above demo, the definition of a static Person class and non-static Animal class, use static modification in the constructor statically defined in the class directly instantiate this class when calling a static class, static methods static class called directly ( before calling on the initialization, static class constructor is executed only once), instantiate the class constructor call this class directly instantiated.