The difference between a static variable and a non-static variable

Source: Internet
Author: User
Tags modifier modifiers

The type descriptor of a static variable is static, and a non-static variable instead of a type specifier is not static. This article mainly introduces the difference between static and non-static variables, I hope to help you to see together.

AD:

static variables : Static variables use the static modifier to declare that the same static variable that creates all instances of the owning class through the class when the owning class is loaded is the same value non-static variable: no static Modifiers declared variables are called non-static variables when a class is instantiated, creating the same non-static variable that accesses different instances of the same class through an object can be a different value.

For an automatic variable, it belongs to the dynamic storage mode. But you can also use static to define it as a static automatic variable, or a static local variable, thus becoming a statically stored mode. In this view, a variable can be explained by static and change its original storage mode.

The following is a description of the difference between a static variable and a non-static variable:

1. Memory allocation

A static variable is present in memory when the application is initialized, until the end of the program at which the class is located, and the non-static variable needs to be instantiated before allocating memory.

2. Life cycle

The static variable lifetime is the lifetime of the application, and the existence period of a non-static variable depends on the lifetime of the instantiated class.

3. Calling mode

Static variables can only be invoked through the class. Static variable name, which cannot be invoked by instances of the class, and non-static variables can be accessed directly through the instantiated class name after the class in which the variable is instantiated.

4. Sharing mode

A static variable is a global variable that is shared by all instances of the class, that is, an instance that changes the value of the static variable, and the other similar instances read the value of the change;
Non-static variables are local variables that are not shared.

5. Access Mode

Static members do not have access to non-static members, and non-static members can access static members.

A static variable allocates memory at the time the class is loaded, and the memory is used by the object that is created later, and the corresponding operation is the operation of the memory. can also be considered as an alternative global variable.

It takes a lot of resources to initialize in Webserivice without having to initialize each time. You can also manage the services that are invoked, such as the need to queue every service you call, and you can put the information you want in the ArrayList and queue it in a static clock.

C # Static constructors and static variables

Static constructors:

(1) For initialization of static fields, read-only fields, and so on.

(2) Add the static keyword and cannot add an access modifier because the static constructor is private.

(3) The static constructor of a class executes at most once in a given application domain: the static constructor is fired only if an instance of the class is created or any static members of the reference class are referenced

(4) A static constructor is not inheritable and cannot be invoked directly.

(5) If the class contains the main method used to start execution, the static constructor of the class is executed before the main method is called.

(6) Any static field with an initializer, when executing the static constructor of the class, first execute those initializers in textual order.

(7) If a static constructor is not written and the class contains a static field with an initializer, the compiler automatically generates the default static constructor.

Classic examples:

*   *  static   state   construction     letter   number   practice   *  (1) ①②③ ... Output results for execution order   *  (2):  static a ()   * static b ()   * x = 1,  Y = 2   */  using system;   Class a   {  public static int x;   static a () to return to ③ after execution   {  x = b.y + 1;   Console.WriteLine ("Static a ()");  }  }   CL Ass b   {  public static int y = a.x + 1; //③  A static member of A is called,  //  to a static constructor---->   static b ()  //②  If a static field with an initializer,  //  When you execute a static constructor for the class,  //  first executes those initializers in textual order.  //  goto initializer---->   {  Console.WriteLine ("Static b ()");  }   static void& nbsp Main ()  //①  program entry,  //  if the class contains the  Main  method used to start execution,  //  the static constructor for the class will be called in the  main  before the method is executed.  //  to B's static constructor---->   {  Console.WriteLine ("X = {0}, y = {1}",  A.X, B.Y);//⑤  output result   console.readline ();  }  } 

a few notes on the C # static constructor:

Static constructors are a new feature of C # that seems to be rarely used. However, when we want to initialize some static variables, we need to use it. This constructor belongs to the class, not to where the instance belongs, which means that the constructor is only executed once. This is called automatically by. NET before the first instance is created or any static members are referenced.

Class Simpleclass {//static constructor static Simpleclass () {//}}

There are a few things you should be aware of when using static constructors:

1. The static constructor has neither an access modifier nor a parameter. Because it is. NET call, so modifiers like public and private are meaningless.

2, when creating the first class instance or any static member is referenced. NET automatically invokes the static constructor to initialize the class, which means that we cannot call the static constructor directly, and we cannot control when the static constructor is executed.

3. A class can have only one static constructor.

4. Parameterless constructors can coexist with static constructors. Although the argument list is the same, but one belongs to the class and one belongs to the instance, there is no conflict.

5, run up to only once.

6. Static constructors cannot be inherited.

7. If no static constructor is written, and the class contains static members with initializer settings, the compiler automatically generates the default static constructor.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.