Summary of the modifiers for classes and member variables and member methods in Java

Source: Internet
Author: User
Tags modifiers volatile

A class of modifiers

The file structure in Java is from large to small: a project, a project can have many packages, each package can have many classes.

The modifiers of the class are divided into access modifiers and non-access modifiers

(1) Access rights modifier:

1 public common class modifiers, which can be accessed by classes in the same package or by classes in other packages. That is, all classes under this project can access classes that are modified by public. The Java language stipulates that the main class of a program must be a public class.

For example, public class Animal {...}

2 default description. A class that does not have a public adornment before it is considered to use the default description, which is known as a friendly class that can only be used by classes in the same package and not by classes in other packages.

For example, class Animal {...}

(2) Non-access modifier:

1 abstract class modifier. Classes that are decorated with abstract are abstract classes. Other classes inherit abstract classes and implement abstract methods in abstract classes. Abstract classes cannot create objects with the new operator.

The 2 final finalization class modifier, which represents the finalization class with the final decorated class, cannot be inherited by the final decorated class, and the class with the abstract modifier is used to inherit from the other class, so a class cannot be both abstract and final decorated.

Modifiers for member variables and member methods in two classes

Member Variable modifiers

(1) Access rights modifier:

1 public access modifier. A variable that is decorated with this modifier is called a public variable, and if the public variable is in a public class (the publicly decorated class), then the variable can be accessed by all classes in all packages;

If the shared variable is in a friendly class (a class that is not publicly decorated), then the variable can be accessed by all classes in the package where the class is located.

2 protected protected Access modifier. If the modifier modifies a member variable in a public class, it can be accessed by the class itself, all classes in the same package, and subclasses of that class in the other package.

3 default access modifier. If there is no access modifier before the member variable, then it is a friendly member and can be accessed by all classes in the same package.

4 private access modifier. The modifier-decorated member can only be accessed by the class in which it is located, and no other class can access it, including its subclasses. In a real project, it is best to set an instance variable of a class (a variable that is not static modified) to private and access it by setting setxxx () and GetXXX () in the method. This helps to detail the implementation of the client's hidden class, reduces errors, and improves the program's modifiable nature.

(2) Non-access modifiers

A variable of 1 static modification is called a static variable, and the static variable belongs to the class. Static variables can be referenced with the class name or the object name of the class

2 Final modified variables are called final variables and are actually symbolic constants in Java

3 volatile modified variables are called volatile variables, which can be controlled and modified by multiple threads.

Modifiers for three member methods

(1) Access rights modifiers

1 Public access control user method that is used as an external interface for this class

2 The default access control character. Has a package access property that can be accessed by other classes within the same package.

3 protect access control protected. The method of using the access modifier can be accessed by the class itself, another class within the same package, and subclasses of that class in the other package.

4 Private access control. The method of using the access control is accessible to the class itself, and any other class cannot be accessed, including subclasses of the class.

(2) Non-access modifiers

1 Abstract The method of abstraction is abstracted, the abstract method has only the declaration of the class, and there is no implementation of the class.

2 static methods are static, static methods belong to the entire class.

3 Final method is final, and the final method cannot be redefined by the subclass of the current class.

4 Native The method that is modified by native is local method, this kind of method usually writes the method body in other language, and realizes the method function.

5 synchronized the method of synchronized modification is the synchronous method, and the synchronization method is mainly used for coordination and synchronization in multi-threaded coexistence programs.

Summary of the modifiers for classes and member variables and member methods in Java

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.