Static study notes

Source: Internet
Author: User
Tags modifiers

Suddenly, I found that the keyword "static" in my mind is a paste. I only know that it is static. I cannot tell you the specific occasions and the advantages and disadvantages of static keyword. As a developer, I am so ashamed... Learn more

StaticModifiers can be used for classes, fields, methods, attributes, operators, events, and constructors, but not for indexers, destructor, or types other than classes.

Static class:

1. Only static members are included.

All members in the static class must be static, and the static class cannot contain instance members.

Constants or type declarations are implicitly static members. In addition, static class members should be added with static modifiers.

Static Class Companyemployee
{
Static String Member;
Const String Member2 = " Test " ;
Public Static Void Dosomething (){ /* ... */ }
Public Static Void Dosomethingelse (){ /* ... */ }
}

2. It cannot be instantiated.

You cannot use the new keyword to create a static class instance.

3. It is sealed

A static class cannot be inherited.

4. The instance constructor cannot be included.

It can contain static constructors. Static constructors can be used for static classes or non-static classes. The static constructor has no access modifier, no parameters, and only one static flag. A static constructor cannot be called directly. A static constructor is automatically executed only once before a class instance is created or any static member is referenced.

 

Static Class Cat
{
Private Static Int Statics = 0 ;
Static CAT ()
{
Statics ++;
}
Public Static Void Howmanycats ()
{
Console. writeline ( " {0} cats adopted " , Statics );
}
}
Public Class Tester
{
Static Void Main ()
{
Cat. howmanycats ();
Cat. howmanycats ();
Console. readkey ();
}
}

Result:

1 cats adopted

1 cats adopted

5. Static and non-static performance comparison

Static slaveProgramThe memory will be occupied at startup, but the non-static memory will be occupied only after instantiation, but each time an object is instantiated, the memory will be occupied.

6. When to use static classes?

When the class does not depend on the data or behavior of object identifiers, you can use static classes. When a class contains data or behavior identified by dependent objects, the static class cannot be used.

Classes that are frequently called should be designed to be static. Classes that are not often called should be designed to be non-static.

Static Member

Static members are usually used to represent data or computing that does not change with the object state. No matter what changes occur to the object, static members do not change.

You can call static methods, fields, attributes, or events in the class without creating an instance of the class.

If a class instance is created, you cannot use the instance to access static members.

Static methods and attributes can only access static fields and static events. Static Methods call non-static methods. An object must be instantiated first.

An important feature of static classes is sharing. Therefore, static variables are usually used to save the number of instances of the current class.

Class Cat
{
Private Static Int Statics = 0 ;
Public CAT ()
{
Statics ++;
}
Public Static Void Howmanycats ()
{
Console. writeline ( " {0} cats adopted " , Statics );
}
}
Public Class Tester
{
Static Void Main ()
{
Cat. howmanycats ();
Cat cat1 = New CAT ();
Cat. howmanycats ();
Cat cat2 = New CAT ();
Cat. howmanycats ();
Cat. howmanycats ();
Console. readkey ();
}
}

Result:

0 cats adopted

1 cats adopted

2 cats adopted

2 cats adopted

 

You cannot use this to reference static methods or attribute accessors.

To access a static member, use the class name instead of the variable name to specify the position of the Member.

Public Class Automobile
{
Public Static Int Numberofwheels = 4 ;
Public Static Int Sizeofgastank
{
Get
{
Return 15 ;
}
}
Public Static Void Drive (){}
Public Static Event Eventtype runoutofgas;

//Other non-static fields and properties...
}

Access statement:
Automobile. Drive ();
IntI = automobile. numberofwheels;

 

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.