Differences between static and non-static

Source: Internet
Author: User

Class ExampleTest
{
Static ExampleTest ()
{
Console. WriteLine ("test static default constructor in unstatic class ");
}

Public ExampleTest ()
{
Console. WriteLine ("test default constructor in unstaic class ");
}
}

Static class ExampleTest2
{
Static ExampleTest2 ()
{
Console. WriteLine ("test static Method in static class ");
}

# Region static classes cannot call instance Constructors

// Public ExampleTest2 ()
//{
// Console. WriteLine ("test unstatic method in static class ");
//}

# Endregion

# Region static classes cannot call instance methods
// Public void getAlert ()
//{
// Console. WriteLine ("Alert, alert ");
//}
# Endregion

Public static void getAlert ()
{
Console. WriteLine ("Alert, alert ");
}
}


From the above two classes, we can draw a conclusion:
1. constructor: whether in a static or non-static class, if we define a static constructor, we only need to create an instance of the class or call the method of the class, will automatically call this Static constructor, and the Static constructor cannot have access permissions. Static constructors cannot have parameters.

2. instance constructors cannot be called in static classes.

2. Non-static methods cannot be created in static classes. That is, only static methods can be created in static methods, but static methods can be called in non-static classes (which is often used in this case)

____________________________________________________________________________________

Static constructor:

(1) It is used to initialize static and read-only fields.

Related Article

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.