Modifiers in Java and their role

Source: Internet
Author: User
Tags modifier modifiers volatile

modifier type modifier Descriptionaccess Control modifiers defaultDefault (that is, default, nothing): It is visible within the same package , without any modifiers. Use objects: classes, interfaces, variables, methods. Private Private : visible within the same class.   working with objects: variables, methods. Note: Classes cannot be decorated (external classes) PublicPublic : visible to all classes . Working with objects: classes, interfaces, variables, methods protected protected : visible to classes and all subclasses within the same package . Working with objects: variables, methods. Note: classes (external classes) cannot be decorated. non-access modifiers StaticStatic variables:
The static keyword is used to declare static variables that are independent of an object, regardless of how many objects a class instantiates, and its static variables have only one copy . Static variables are also known as class variables. A local variable cannot be declared as a static variable. static method:
The static keyword is used to declare an object-independent method. static methods cannot use non-static variables of a class . The static method obtains the data from the parameter list and then computes the data. FinalFinal variable:
The final variable can be explicitly initialized and initialized only once. A reference to an object that is declared final cannot point to a different object. But the data in the final object can be changed. This means that the final object reference cannot be changed, but the value inside can be changed .
The final modifier is typically used with the static modifier to create a class constant. Final method
the final method in the class can inherit from the quilt class, but cannot be modified by the quilt class .
The primary purpose of declaring the final method is to prevent the content of the method from being modified. Final Class
The final class cannot be inherited, and no class can inherit any of the attributes of the final class. AbstractAbstract class:
An abstract class cannot be used to instantiate an object, and the sole purpose of declaring an abstract class is to augment the class in the future.
A class cannot be both abstract and final decorated . If a class contains an abstract method, the class must be declared as an abstract class, or a compilation error will occur.
Abstract classes can contain both abstract and non-abstract methods. abstract Methods
An abstract method is a method that does not have any implementation, and the specific implementation of the method is provided by the subclass.
Abstract methods cannot be declared final and static.
any child class that inherits an abstract class must implement all the abstract methods of the parent class, unless the subclass is also an abstract class .
If a class contains several abstract methods, the class must be declared as an abstract class. Abstract classes can contain no abstract methods.
The declaration of an abstract method ends with a semicolon, for example: Public abstract sample ();. synchronized The method of synchronized keyword declaration can only be accessed by one thread at a time . The synchronized modifier can be applied to four access modifiers. transient When a serialized object contains an instance variable that is transient decorated, the Java Virtual Machine (JVM) skips that particular variable .
The modifier is included in the statement that defines the variable, which is used to preprocess the data type of the class and variable. volatileOlatile-Modified member variables force the value of the member variable to be re-read from shared memory each time it is accessed by the thread. Also, when a member variable changes, the thread is forced to write the value of the change back to the shared memory. So at any moment, two different threads always see the same value for a member variable.
A volatile object reference may be null.


Modifiers in Java and their role

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.