Java Learning (eight): Java modifiers (classes, variables, methods)

Source: Internet
Author: User
Tags modifiers

Modifiers in Java are categorized as class modifiers, variable modifiers, and method modifiers.

Class modifier:

1, Public:public is the public class.

2. Package: The default defaults to pack modifiers, this package is visible.

3. Abstract: A class that is decorated with an abstract modifier is called an abstract class. A class that contains one or more abstract methods must be declared as an abstract class, and an abstract class cannot be instantiated.

4. Final: When a class cannot have subclasses, the modifier final is used to describe it as the final class.

Variable modifiers:

1. Public: Variables modified with public can be referenced by all other classes.

2, Private: The member variable (domain) modified with private can only be accessed by the class itself, and cannot be referenced by any other class (including subclasses).

3. Protected: Member variables modified with protected can be referenced by three species: ① the class itself, ② other classes in the same package, ③ subclasses of the class in other packages. The primary purpose of using the modifier protected is to allow its subclasses in other packages to access the specific properties of the parent class.

4, Package: The default access rights, packet access rights.

5, Static: the member variable modified with static only belongs to the variable of the class, but not to any specific object, the value of the static member variable is the common storage unit in the memory area of the class, not the memory interval of the object that is saved. When an object of any class accesses it, it is the same data, and when any object of the class modifies it, it also operates on the same memory unit.

6. Final: Final domain modifier final is used to define symbolic constants. The final member variable of a class that cannot be modified after initialization.

7, transient: temporary domain modifier transient is used to define a transient variable. Variables decorated with this modifier in serialization will not be serialized.

Method Modifiers: Public, private, protected, default package access permissions.

1. Abstract: The method of using modifier abstract modification is called abstract method. Abstract methods do not have method bodies, and require subclasses to implement specific operations, often used in polymorphism.

2. Static: A method called static method with modifier static modifier. A static method is a class method that belongs to the entire class, rather than a static decorated, qualified method that belongs to a specific class object. Because the static method belongs to the entire class, it cannot manipulate and process member variables that belong to an object, but only member variables that belong to the entire class, that is, the static method can handle only the static domain.

3. Final: The method of final modification with modifiers is called the ultimate method. The final method is that the functionality and internal statements cannot be changed, i.e. the final method cannot be overloaded. In this way, the function and operation of this method are fixed, and the error definition of the parent class's key method is prevented by the subclass of the current class, which guarantees the security and correctness of the program. All methods that are qualified as private by the private modifier, and all methods contained in the final class (The final Class), are considered the final method.

4. Synchronized: Locks the current object when the Synchronized method (or code block) is called. This modifier is primarily used for coordination and synchronization in a multithreaded coexistence program.

Java Learning (eight): Java modifiers (classes, variables, methods)

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.