Writing high-quality code: 151 recommendations for improving Java Four (classes, objects, methods) 31-51

Source: Internet
Author: User

Tag: class refactoring static class improvement pre some SYS col JSON

No implementation code exists in the interface

The implementation code cannot exist in the interface (although it can be implemented, but if the implementation code is written in the interface, then the interface is bound to a possible change of factors, which results in the implementation is not document and reliable, can be discarded at any time, modified, reconstructed)

 Packagejsontest; Public classSalary { Public Static voidMain (string[] args) {b.a.dosomething (); }}Interfacea{ Public voiddosomething ();}Interfaceb{ Public Static FinalA A =NewA () { Public voiddosomething () {System.out.println ("Implementation method in---------interface"); }    };}

32. Static variables must first be declared after assigning values

  Static variable loading process: The static variable is first loaded when the class is initialized, the JVM will find all the static declarations in the class, and then allocate space, this time just to complete the allocation of the address, has not been assigned, the JVM will be based on static assignment in the class (Static class assignment and static block assignment) in the order of execution. A static variable has only one copy in memory, so the assignment operation changes its value and the address does not change.

33.

Writing high-quality code: 151 recommendations for improving Java Four (classes, objects, methods) 31-51

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.