The similarities and differences between Java's Static and C # 's static

Source: Internet
Author: User
Tags modifier

Static

Static can also be used in classes, methods, variables above, but in Java and C # in the meaning of the completely different, my personal summary is that C # static and non-static is a clear demarcation, static belongs to the class level, rather than static is at the instance level. In Java it's not so much, meaning in Java is meant to be shared by all instances, because the compiler opens up only one memory space for static members.

static modifier class :

In Java if static is present in front of class, then this class must be a nested static class. Because the outer class cannot be modified with static. In C #, the outer class can be statically decorated, representing a class that cannot be instantiated, and the inside of a class cannot have non-static members, and cannot have constructors (because they cannot be instantiated). static classes in C # are more used for extension methods.

At the level of nested classes, Java and C # can have static nested classes. There is nothing wrong with the syntax, and there can be non-static methods in a static nested class in Java, and static nested classes in Java can be instantiated as well. C # No, C # static classes cannot be instantiated.

These are some of the things that go around, to summarize: Whether in Java or C #, only one class is static decorated, then, in the scope of this class is a static context, a statically, and in this static context, Java can not access non-static members, C # can neither access nor declare non-static members.

Static Modification Method : A non-static method can access both static data members and non-static data members, while static methods can access static data members only, static methods can gain access to both statically and non-static methods, and static methods can only access static data methods. Because static methods and static data members are allocated and loaded into memory as the class is defined, not static methods and non-static data members are only snippets of this method in memory of the object when the class object is created.

Static modifier variables: staticmodified variables behave almost like in C # and Java, are class-level, and a slight difference is that C # static members must be referenced by the "class name. Static Member" method, and Java can also be used The "object name. Static Member" method to refer to.

Static members are allocated and loaded into memory as the class is defined, and the compiler creates only one copy of the static variable for the entire class, that is, only one memory space is allocated, although there may be multiple instances, but these instances share the memory, and it is particularly noteworthy that Any modification of an object to a static data member can affect other objects.

The similarities and differences between Java's Static and C # 's static

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.