Java Fundamentals: Member variable access control and final keyword

Source: Internet
Author: User

Use Java to do the project, but find a lot of Java Foundation is not very clear. (Learn a little bit about it)

Keyword: public protected private default final

Public: All places are available,

Protected: Can be used in the same class, or in the same package, or a different package but is his subclass,

Private: Can only be used in the same class inside (may be internal classes OH),

Default: The same class inside, or within the same package.

The function of the final keyword is to think of a constant that cannot be changed (so it is initialized at the time of definition), but what is not changed is that the value of the underlying type cannot be changed, whereas for a modified object variable it is said that its reference cannot be changed (he can also initialize it in the constructor).

     Declare method as final inline mechanism, it will make you call the final method when the method body is inserted directly into the call, Instead of making routine method calls, such as saving breakpoints, pressing stacks, and so on, this may make your program more efficient, but when your method body is very large, or you call this method in many places, then your calling body code will quickly expand, may affect efficiency, so you should use caution with the Span style= "font-family:arial;" >final for method definition.

when you will when final is used on a class, you need to think about it because a final class cannot be inherited by anyone, which means that this class is a leaf class in an inheritance tree, And this type of design has been considered perfect without the need for modification or expansion.

Java Fundamentals: Member variable access control and final keyword

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.