difference between static and non-static members

Source: Internet
Author: User

Declaring a member in a class as static is a member of a static member class, or static, or non-static, in general, a static member is a non-static member belonging to a class and is an instance object of a class. The following example code demonstrates how to declare a static and Non-static member

Program Listing 10-5

Using System;

Class Test

{

int x;

static int y;

void F ()

{

x = 1; Correct, equivalent to This.x = 1

y = 1; Correct, equivalent to Test.y = 1

}

static void G ()

{

x = 1; Error cannot access this.x

y = 1; Correct, equivalent to Test.y = 1

}

static void Main ()

{

Test T = new Test ();

T.x = 1; That's right

T.Y = 1; Errors cannot access static members in instances of a class

Test.x = 1; Error cannot access non-static members by class

Test.y = 1; Correct}}

A non-static member of a class belongs to an instance of a class. All instances of creating a class open an area in memory for Non-static members, and the class's static members belong to all instances of the class, regardless of how many replicas the class creates. A static member occupies only one area of memory.

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.