Java _ static code block

Source: Internet
Author: User

Static: static usage: it is a modifier used to modify members (member variables, member functions ). when a member is modified statically, an additional call method is provided. In addition to being called by an object, the member can also be called directly by the class name. Format: Class Name. static member. Static features: 1. Loading with classes. That is to say: static will disappear with the disappearance of the class. It indicates that the lifecycle is the longest. 2. objects with a higher priority have a clear point: static objects exist first. Objects exist. 3, shared by all objects 4, can be called directly by class name. Differences between instance variables and class variables: 1. storage location class variables exist in the method area as the class is loaded. Instance variables exist in heap memory as objects are created. 2. The maximum lifecycle of a class variable disappears as the class disappears. The lifecycle of instance variables disappears with the disappearance of objects. Static usage considerations: 1. Static methods can only access static members. Non-static methods can both access static and non-static methods. 2. this and super keywords cannot be defined in static methods. Because the static state takes precedence over the object existence. Therefore, this cannot appear in static methods. 3. The main function is static. Static Advantages and Disadvantages: saves space by storing the shared data of objects separately. There is no need to store a copy of each object. Can be called directly by class name. Disadvantage: the lifecycle is too long. Access restrictions. (Although static is good, only static access is allowed)

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.