Access modifiers and the use of optional modifier static

Source: Internet
Author: User
Tags modifiers

In Java, there are many modifiers, here is a brief talk about the choice of several access modifiers to use, and optional modifier static use.

I. Access modifiers: This is the access to the methods and variables in Java that control access to classes and classes, to expose only the interface, to hide the details of the internal implementation, the access control is divided into 4 levels (only applicable to class and class members decorated with private, default, protected , public), and then briefly introduce their selection in the form of a table:

This class Same package Different packages
Sub-class Non-sub-class Sub-class Non-sub-class
Private X X X X
default x x
protected x
Public

Description: Inside the table, √ indicates the scope that can be accessed with this modifier;

X means that the range that is not accessible can be decorated with this modifier;

Two. Use of optional modifier static:

 1.static modifier Properties (variables):

Use (example: public static String name;) Do not use (example: public String name;)
The grammatical It can be accessed with a dot of the object, but it is more directly accessed by the class name. This property must be accessed with a dot of the object.
On-Memory This property is not placed on the object, but instead exists in the static area of the data segment and is shared by the whole class. This attribute is placed on each object (each object has its own name property)
The grammatical This property is related to the class and is independent of the object. This property is part of an object
Variable generation time is when the class is loaded. is created at the time of the new object.
Constant property Do not consider the direct design: public static final ...; -----------------------------------------
Property type Static properties, class properties Normal property, non-static property

  2.static Modification Method:

Use Do not use
The grammatical Although it can be called with an object, it is more directly called with the class name Must be called using an object
Operation on Static methods can only manipulate static static properties and other static methods they invoke, and cannot use This/super Both static and non-static properties can be manipulated, either static or non-static methods can be manipulated
Load on Static method is loaded first Non-static method is loaded after
Use the A method has nothing to do with the object, it is only designed to use static, If a method is related to an object, it can only be designed as a non-static
Design on If the method is a tool class, the tools method should be designed as static, If the implementation part of the method needs to manipulate properties or other methods related to the object, the method should be designed as a non-static

 3. Mechanism:static method is loaded first, so non-static content cannot be manipulated.

 Common features of 4.static:

① all static are related to the object, are class-level.

② usually static in the loading period will have special treatment

③ constructor method cannot use static

④static modified in the static area of the data segment at load time

Access modifiers and the use of optional modifier static

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.