Java Basics-access modifiers

Source: Internet
Author: User
Tags modifier modifiers volatile

Access modifiers

Default (that is, default, nothing): It is visible within the same package, without any modifiers. Use objects: classes, interfaces, variables, methods.
Private: Visible within the same class. Working with objects: variables, methods. Note: Classes cannot be decorated (external classes)
Public: Visible to all classes. Working with objects: classes, interfaces, variables, methods
Protected: Visible to classes and all subclasses within the same package. Working with objects: variables, methods. Note: Classes (external classes) cannot be decorated.

Default access modifier

Variables and methods declared with the default access modifier are visible to classes within the same package. The variables in the interface are implicitly declared as public static final, and the methods in the interface are public by default.
Non-access modifiers
Java also provides a number of non-access modifiers in order to implement some other functionality.
The static modifier, which is used to modify class methods and class variables.
Final modifiers, which are used to modify classes, methods, and variables, the final decorated class cannot be inherited, the decorated method cannot be redefined by the inheriting class, the modified variable is constant, and is not modifiable.
An abstract modifier that is used to create abstract classes and abstract methods.
Synchronized and volatile modifiers, which are used primarily for threading programming.

static modifier

Static 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.
Final modifier
The final meaning of the decorated class, method properties, variables
The final decorated class does not allow the class to be inherited
The Final decoration method does not allow the method to be inherited
The Final decorated property does not implicitly initialize the class (the initialization property of the class must have a value) or be assigned in a constructor method
Final modified variable, the value of the variable can be assigned only once, which becomes a constant

Abstract modifier

Abstract 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 method:
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.

Other modifiers

Synchronized modifier
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 modifier
When a serialized object contains an instance variable that is transient decorated, the Java Virtual Machine (JVM) skips that particular variable.
volatile modifier
A volatile-modified member variable forces 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.

Java Fundamentals-access modifiers

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.