static method in Java

Source: Internet
Author: User

Learned today and applied to static methods in Java, and learned about its advantages and disadvantages.

life cycle (Lifecycle):

Static methods , like static member variables, belong to the class itself and are loaded into memory when the class is loaded, not automatically destroyed, and will persist in memory until the JVM shuts down.

non-static method ( Non-static method), also known as an instance, is an instance object that allocates memory after instantiation and must be referenced by an instance of the class. Memory is not resident, and then disappears after the instance object is reclaimed by the JVM.

storing locations in memory

static methods and static variables always use the same block of memory after they are created, and are contiguous.

non-static methods exist in multiple places of memory and are discrete.

Efficiency

The efficiency of static methods is higher than that of non-static methods.

Thread Safety

A static method is a shared code segment, and a static variable is a shared data segment. Since it is "shared," there is an issue of concurrency (concurrence).

non-static methods are targeted at an object that is determined, so there is no thread-safe issue.

Scope of Use

static methods : ⒈ classes that have static properties, typically define static methods. ⒉ a class that has no attributes, a static method is typically defined. ⒊ If a method is not related to an instance object of his class, then it should be static. static methods can be inherited but cannot be overwritten .

Total

If a static method is defined too much in the system, it consumes a lot of resources and eventually causes memory overflow, so static methods cannot be abused. If you look at thread safety, performance, and compatibility, it is advisable to choose an instantiation method.

static method in Java

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.