Java-based keyword 2 (package, class, import) and (static, Fina) and (public, protected, private)

Source: Internet
Author: User
Tags modifiers

(5) package-related

Class: Use the class keyword to create a new class.For example:
Class aclassname
{
Class body goes here
}
The words modifying class include public, abstract, and final. abstract and final cannot simultaneously modify class. Internal classes can also be modified using private or protected. You can set two types of elements in the class: fields and methods, collectively referred to as members of the class. Class, source file, and compiled file: a Java source code file (usually called a compilation unit) can have one and only one public class, the name of this class must be the same as the file name (including case, but not the file Suffix :. java ). When compiling a. Java file, each class in the. Java file will have a. Class output file with the same class name.
Although it is not very common, it is also possible that there is no public class in the compilation unit. In this case, you can name the file at will.

Package: to uniquely identify and group each classJava uses the Package concept. At the same time, you can put the logic-related classes in the same package to make the program structure clearer. All you need to do is add a line at the beginning of the Java File

Package mypackage;

Note that the package statement must be the first line of uncommented program code in the file.
By convention, the first part of a package name is divided into the reverse order of the Internet domain name of the class creator, which is conducive to the uniqueness of the package name.
Whenever a package is created, the directory structure is implicitly specified while the package name is specified.
Note that the packages are not nested or contain relationships. Packages A and A. B are two parallel packages for Java commands.

Import: import a library with the import keywordTo make it an available unit.
Classes in the Java. lang package are imported by default.
The full name can be used to specify classes that use other packages in a class, such as Java. util. arraylist (so that the import statement is not used), or you can specify it as arraylist (due to import ).

**************************************** **************************

(2) classes, methods, and variable Modifiers

Static: used to modify fields and methods.
Generally, we create a class object to access its fields and methods, declaring a field or method as static means that this field or method exists as the whole class rather than a specific object of the class. You can directly reference them using the class name.
A static field has only one bucket for each class, while a non-static field has a bucket for each object.
An important usage of the static method is to reference it directly through the class without creating an object.
Popular saying:
1. If a member without static modification is an object member, it is owned by each object.
2. The member with static modification is a class member, which can be directly called by a class and is shared by all objects.

Final: used to modify fields, methods, and classes.
Final modifier field. For basic types, final makes the value constant, and for object reference, final makes the reference constant. Once the reference is initialized to an object, it cannot be changed to another object. However, the object itself can be modified. A static and final field only occupies a bucket that cannot be changed. The basic types of static final with a constant Initial Value (that is, the compile time) are all named in uppercase letters and separated by underscores.
1.Blank final refers to a field declared as final but not given an initial value. This field is initialized at runtime and remains unchanged in the future. The final parameter means that you cannot change the object pointed to by the parameter reference in the method. You can reference the parameter but cannot change it.
2.Final modification method. There are two reasons for using the final method. The first reason is to lock the method to prevent any inheritance class from modifying its meaning. This is designed to ensure that the behavior of the method remains unchanged in inheritance and will not be overwritten. The second reason for using the final method is efficiency. If you specify a method as final, you agree that the compiler will convert all calls to the modified method into embedded calls, And the compiler will choose whether to execute embedded calls to the final Method Based on the overhead. All private methods in the class are implicitly specified as final.

3.Final modifier class. Defining a class as final indicates that the class cannot be inherited.
Final fields can be selected or not final based on your wishes. The same rules apply to final fields, regardless of whether the class is defined as final.
All methods of the final class are implicitly specified as final.

**************************************** **************************

(1) Access Control

Public, protected, and private are used as Java access permission modifiers.

First, you must understand that each definition (definition class, field, and method) in Java has specific access permissions. If no modifier is added, the package access permission is granted by default, that is, the classes in the package have access permissions to them.

1.All public classes can be accessed.

2.Protected inherits the access permission. The base class grants the access permission to the derived class instead of all classes through protected. In addition, protected also provides the package access permission, that is, other classes in the same package can access the protected element.

3.In addition to the class containing this Member, private cannot access this member in any other class.

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.