Why static methods are used

Source: Internet
Author: User

The advantage of a static method is that it can be called directly without generating an instance of the class.

The member that is decorated by the static method no longer belongs to an object, but to the class in which it resides. It needs to be accessed through its class name, without the need to consume resources to create objects repeatedly.

When the class first loads, the static is already in memory, and the memory is not released until the end of the program.

If it is not a static decorated member function, it will be reclaimed by the JVM immediately after it is exhausted.

When do I use static?

If this method is used as a tool, it is declared as static and does not need to be used by a new object. For example: Connect DB can declare a connection () static method,

Because it is static, connection db is not a feature specific to an object, just as a tool to connect to the DB

Why static methods are used

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.