Java access Control modifiers

Source: Internet
Author: User
Tags modifiers

Access Control modifiers

In Java, you can use access controls to protect access to classes, variables, methods, and construction methods. JAVAV supports 4 different types of access rights.

    • default (That is, by default, nothing is written): it is visible within the same package, and no modifiers are used. 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.

We can use the following table to illustrate access rights:

Access Control
modifier Current Class in the same package descendant class Other Packages
public Y Y Y Y
protected Y Y Y N
default Y Y N N
private Y N N N
1 classAudioplayer {2    protected BooleanOpenspeaker (Speaker sp) {3       //Implementation Details4    }5 }6  7 classStreamingaudioplayerextendsAudioplayer {8    protected BooleanOpenspeaker (Speaker sp) {9       //Implementation DetailsTen    } One}

If the Openspeaker () method is declared private, classes other than Audioplayer will not be able to access the method.

If you declare Openspeaker () as public, all classes have access to the method.

If we only want the method to be visible to subclasses of its class, declare the method as protected.

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.

    • A static variable does not mean that it cannot change the value, and the amount of the value cannot be changed is called a constant. The value it has is mutable, and it maintains the most recent value. It is static because it does not change as the function calls and exits. That is, when the function was last called, if we assign a value to a static variable, the value remains unchanged the next time the function is called.

Java access Control modifiers

Related Article

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.