Java Development Manual The 7th chapter of the Learning process access control

Source: Internet
Author: User

The access control for the class:

There are four types of access controls, but for external classes , only the public and the default are sufficient.

Public means that the class can be used by any class.

When a class is defined as public, it can be accessed in any class.

Access control for Members:

The ability of members (that is, member variables and methods) to be accessed by other classes depends primarily on two factors:

  • The visibility of the class of the member variable and the method is the precondition that it can be accessed normally;
  • Second, the access control of the member variables and methods directly determines whether they can be accessed.

Static keyword:

There is no concept of global variables in Java.

If static modifies a member variable, all instances of the class share this variable when the object of the class is generated, and the variable can be accessed before the object is created. This variable can be initialized only at the time of definition or in a static block of code .

A static code block is represented by a statically decorated block of code, and when the JVM loads the class, it executes the code block and executes only once. If there are multiple static code blocks, the JVM loads the classes in the order in which they appear in the class.

When a member variable is modified to be final rather than static, it belongs to the instance variable of the class (as opposed to a static variable), and when the class is loaded into memory, the property does not allocate memory space, but only defines a variable that is allocated memory space only when the class is instantiated. The constructor is executed at the same time as the instantiation, so the property is initialized, conforming to the condition that the allocated memory space needs to be initialized and no longer changed.

When a member variable of a class is both modified to static and final, it belongs to a class variable. When a class is loaded into memory, it allocates memory for this variable because it is also final decorated, giving it an initialization value after the variable is defined. The constructor is executed only when the class is instantiated, so you cannot initialize the static and final decorated member variables with constructors (otherwise there will be contradictions), and the member variable can still be initialized in the static block.

The static method can be called directly from the class name, and any instance can also be called (this is consistent with the static member variable). However, you cannot use the This and Super keywords in the static method, you cannot directly access the instance variables and instance methods of the owning class, only the static member variables and static methods of the owning class.

Because the static method is independent of any object, the static method must be implemented, not abstract.

Static is used to modify member variables and member methods, and does not apply to local variables.

Final variables:

Variables and object references for basic data types do not have the same meaning when they are final decorated.

An inner class defined in a method must be final when the local variable of the method is used.

Use of the package:

      • Java.lang Package: A Java infrastructure package that provides a variety of base classes for the basic structure of the Java language, such as object,string.
      • Java.util Package: A Java toolkit that provides tool classes such as encoding, decoding, hash tables, vectors, and stacks.
      • Java.io Package: Provides the standard input stream, output stream, and file access related tool classes.
      • Java.applet Package: A small application package that provides classes to interact with a Java-enabled browser.
      • java.awt Package: A window toolkit that provides graphical user interface tool classes such as fonts, base events, listeners, panels, forms, buttons, and scroll bars. However, the functionality provided by this package has been replaced by another more powerful tool.
      • java.net Package: Network packet, provides support network communication class, such as socket class, support Telnet, FTP and WWW and other protocols to access the network.

The package name is generally lowercase, and the letters in the class name are capitalized, so that the package name and the class name can be distinguished clearly when referencing.

by statically introducing a package, you can directly reference a static member method or static member variable of a class. For example, an import static Java.lang.Math.PI uses the import statics of the class to be used in the passive resource, then the PI instead of the Math.PI can be used directly in the next code. In addition, you can represent all static resources under the introduction class by using "*".

Static introduction is convenient, but because the method may affect program performance on a large program, try to use a static variable or method when the frequency of access is very large.

Java Development Manual The 7th chapter of the Learning process access control

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.