Static classes in. NET and internal static classes in Java

Source: Internet
Author: User

The second article in objective Java yesterday: when encountering multiple constructor parameters, consider using constructor. In this example, the builder mode is used to build complex objects. This is because I was not familiar with Java. Q: Can I build an instance with static class? Previously, static classes in. Net had never been instantiated.

After checking the information, only the internal static classes in Java can be instantiated, which is usually called Nested classes. The top-level class, that is, the class that is not nested in another class, cannot be marked as static. This is different from. net. In fact, I personally think that both languages have their own concerns.

In terms of functionality, Java's external classes cannot be marked as static, and there is no inconvenience in development. Generally, whether it is a member or a variable, if it is marked as static, it is owned by the class and has nothing to do with the class instance. Therefore, I think there are few requirements and a static class needs to be built. In. net, static classes mean that all the Members in the class are static. Due to such restrictions, static classes in. NET are usually used as tool classes. However, in Java, the methods contained in a non-static class are static. Cannot the same function be implemented? I think there is nothing missing in Java.

However, is it a waste of time for. Net to do so? In fact,. Net also has its considerations, but it is not functional .. A class in net is identified as static, which means all the members of this class must be static. This static check will be performed during compilation and will be restricted by the mandatory during compilation.ProgramMembers follow the rules. If a class is identified as static, the static class will be loaded when the Assembly is loaded until the Assembly is uninstalled. In addition, it is said that CLR has optimized it. I think the same may be the JVM. I don't know much about it for the time being.

However, internal static classes in Java can be used to build instances. In my opinion, it is quite awkward to understand. However, it is much easier to understand the internal classes in Java as their members. The internal static class can be considered as a static type of the external class, not an instance of the external class. For more information about the relationship between the internal static class and the external class, there are also a lot on the Internet. Here we just want to compare the differences between the two languages.

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.