In languages, what do you know about static and non-static methods?

Source: Internet
Author: User

Problem Source: an interesting problem was found yesterday when I gave my younger brother a tutoring of the data center charging system. It is BLL and Dal layers. layer D is a static method. When layer B calls the method of layer D, it is called directly.

At that time, I saw this problem. I felt like I had to think about it according to the conventional thinking. I must first create a new layer D and then call it. It's a bit confusing to think about this problem.

After some understanding:

I. The method is written every day. Many programmers mostly use the instantiation method, but seldom use the static method. I cannot tell why, or simply answer the difference between the two definitions. The static method can be used only after the new method is needed without the new method .... Do we really understand this?

2. From the perspective of actual project development, the answer to this question is as follows:

In the Development Project, BLL and Dal are separated and BLL calls the Dal code.

1. Use static methods in the dal and call them directly without creating instances (this method is probably used by many people)

Class Dal {public static string GetUserName (...);} is called in BLL: Dal. GetUserName ();

2. Use the instance method in the dal and use the static member mode (or Singleton) to call the instance:

Class Dal {public static readonly Dal = New DAL (); Public String GetUserName (...);} is called in BLL: Dal. Dal. GetUserName ();

3. Use the instance method in the Dal. Create an instance of the Dal object before each call:

Class Dal {Public String GetUserName (...);} is called in BLL: Dal = New DAL (); Dal. GetUserName ();

Analysis of the above example:

Development Method 1: I think thread security is suspected in some situations (such as calling multiple databases and GetUserName (...) internal processing operations. This development method does not need new output objects, so it is common.

Development Method 2: most of the applications should be in CS mode, and Dal only has one object in the entire project. If it appears in B/S, I don't think it is compatible with many problems. There are also thread security issues.

Development Method 3: it should be widely used and compatible with various problems, without the suspicion of thread insecurity.

Analyze from memory

Because of the problem above and the following problem, when the program runs for a while, it is reported that the connection exceeds the maximum number of connections. What is the problem?

Cause: the biggest difference is the memory. Static methods generate memory at the beginning of the program, and the instance method generates memory in the program running. Therefore, static methods can be called directly. The instance method must generate an instance first, and the static speed is fast through the instance call method, however, the memory will be occupied if the number is increased.

Any language is used for memory and disk operations. As to whether it is object-oriented, it is only a software layer problem. The underlying layer is the same, but the implementation methods are different. Static Memory is continuous, because it is generated at the beginning of the program, and the instance applies for discrete space, so of course there is no static method fast, and static memory is limited, too many programs will not start.

Comparison between static and non-static methods:

① Static members belong to the class and non-static members belong to the class instance.

② Each time a class instance is created, a new storage block is allocated to non-static members in the memory;

Non-static members belong to the class and are shared by instances of each class. No matter how many instances are created for the class, static members of the class only occupy the same region in the memory.

Summary: everyone has a consensus on this issue: that is, the instantiation method is more used and secure, and the static method is less used. The instantiation method is recommended for thread security, performance, and compatibility.

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.