Java modifier and java Modifier

Source: Internet
Author: User

Java modifier and java Modifier

The Java language provides many modifiers, mainly divided into the following two types: access modifier and non-access modifier; modifier is used to define classes, methods, or variables, usually placed at the front of the statement.

1. Access Control Modifier

In Java, access controllers can be used to protect access to classes, variables, methods, and constructor methods. Java supports four different access permissions:

1. default → default, Visible in the same package, without any modifiers.

Default access modifier-No keywords are used. Variables and Methods declared by default access modifiers are visible to classes in the same package. All the variables in the interface are implicitly declared as public static final, and the access permission of the methods in the interface is public by default.

Public static void main (String arg []) {

//......

}

Note: The main method in the java program must be set to public static. Otherwise, the java interpreter cannot run this class.


4. protected → protected, Visible to the classes and all subclasses in the same package.

Variables, methods, and constructors declared as protected can be accessed by any other class in the same package, and can also be accessed by sub-classes in different packages. The protected access modifier cannot modify classes and interfaces, methods and member variables can be declared as protected, but the member variables and member methods of the interface cannot be declared as protected.

Subclass can access the methods and variables declared by the protected modifier, so as to protect irrelevant classes from using these methods and variables. The following parent class uses the protected access modifier. The subclass reloads the openSpeak () method of the parent class:

Class AudioPlayer {
Protected Boolean openSpeaker (Speaker sp ){
// Implementation details;
}
}
Class StreamingAudioPlayer {
Boolean openSpeaker (Speaker sp ){
// Implementation details;
}
}

If you declare the OpenSpeak () method as private, classes other than AudioPlayer cannot access this method. If openSpeak () is declared as public, all classes can access this method. If we only want this method to be used to subclass Courseware of its class, declare this method as protected.

Note:

1) The method declared as public in the parent class must be public in the subclass;

2) The method declared as protected in the parent class is declared as either protected or public in the subclass, and cannot be declared as private;

3) The method declared as private in the parent class cannot be inherited.

 

2. Non-access modifier

To implement some other functions, Java also provides many non-access modifiers. Static modifier, Final modifier, Abstract modifier, and so on.

1. static Modifier

Used to create class methods and class variables. Static, can be called dynamically without being converted into an object, but can be called statically directly in the form of a class; static member attribute values are shared by all objects of this class, whether you modify a class or modify the static attribute value of an object of this class, the static attribute value of all objects of this class will be modified. The non-static members of this class cannot be directly referenced in static methods, to be referenced, you must either change non-static members to static members or convert the class instance into an object and dynamically reference it as an object.

// In the above example, instantiate x as an object () or static int x → change x to static;

 

 

 

 

 

// Static members can be called dynamically, but dynamic members cannot. You can use A. y instead of A. x;

 

 

 

1) static variables: the static keyword is used for static variables independent of objects. No matter how many objects a class instantiates, the static variables only have one copy. static variables are also called class variables, local variables can be declared as static variables;

2) static Methods: static keywords are used to declare static methods independent of objects. static methods cannot use non-static variables of classes. The static method obtains data from the parameter list and then computes the data.

 

2. Final Modifier

It is used to modify classes, methods, and variables. final modified classes cannot be inherited, modified methods cannot be redefined by the inherited classes, and modified variables are constants and cannot be modified.

1) final variable: The final variable can be explicitly initialized and can only be initialized once. References to objects declared as final cannot point to different objects, however, the data in the final object can be changed, that is, the reference of the final object cannot be changed, but the value can be changed. The final modifier is usually used together with the static modifier to create class constants. For example:

Public class Test {

Final int value = 10;

Public static final int BoxWidth = 6; // declare a constant

Static final String Title = "Manager ";

 

Public void changeValue (){

Value = 12; // an error is returned.

}

}

2) final method: Methods in the class can be inherited by the quilt class, but cannot be modified by the quilt class. The declared final method mainly aims to prevent the content of the method from being modified. For example:

Public class Test {

Public final void changeName (){

// Method body

}

}

3) final class: The final class cannot be inherited, and no class can inherit any features of the final class. For example:

Public final class Test {

// Class body

}

Note: once final is declared, it cannot be modified. It can only be assigned at the time of declaration or in the constructor. It cannot be assigned anywhere else.

 

3. Abstract Modifier

Used to create abstract classes and abstract methods. Abstract: modifies only member methods. Indicates that the method does not have the content of the method subject (even the braces {} are not ). All classes that include abstract methods must be abstract classes. class modifiers must be added before class abstract. Abstract classes cannot be converted into objects. Purpose: To Inherit the outline of the subclass columns of this class, the subclass must implement all methods before it can be turned into an object.

1) abstract class

Abstract classes cannot be used to instantiate objects. The sole purpose of declaring an abstract class is to expand the class in the future. A class cannot be simultaneously modified by abstract and final. If a class contains an abstract method, the class must be declared as an abstract class. Otherwise, a compilation error occurs. The abstract class can contain abstract methods and non-abstract methods. For example:

Abstract class Caravan {

Private double price;

Private String model;

Privete String year;

Public abstract void goFast (); // abstract Method

Public abstract void changeColor ();

}

2) Abstract METHODS

An abstract method is a method without any implementation. The specific implementation of this method is provided by sub-classes. abstract methods cannot be declared as final or strict, any subclass that inherits the abstract class must implement all abstract methods of the parent class, unless this subclass is also an abstract class.

If a class contains several abstract methods, the class must be declared as an abstract class and can not contain abstract methods. The declaration of abstract methods should end with a semicolon, for example, public abstract sample ();

Public abstract class Superclass {

Abstract void m (); // abstract Method

}

Class SubClass extends SuperClass {

Void m (){

...... // Implement the abstract Method

}

}

4. other modifiers(Mainly used for Thread Programming)

1) SynchronizedModifier

Synchronous, only the Member method can be modified, indicating that the method needs to wait for the pipe process in a multi-threaded running environment. The method declared by the Synchronized keyword can only be accessed by one thread at a time. The Synchronized modifier can apply four access modifiers. Example:

Public synchronized void showDetail (){

......

}

2) TransientModifier

It is not a part of the persistent state of an object. It only modifies member variables. When a serialized object contains an instance variable modified by transient, the Java Virtual Machine (JVM) skips the specific variable. The modifier is included in the statement defining the variable, data types used for preprocessing classes and variables.

 

3) volatileModifier

Each time a member variable modified by Volatile is accessed by a thread, the value of the member variable is forcibly re-read from the shared memory. In addition, when the member variables change, the thread is forced to write the change value back to the shared memory. In this way, two different threads always see the same value of a member variable at any time. A volatile object reference may be null.

 

4) nativeModifier

Naturally, only the Member method can be modified, indicating that the method can be called by java, but not written in java.

 

Appendix:

Class modifiers: public, abstract, final, protected, private, default (none );

Member modifiers: static, final, abstract, native, synchronized, transient, volatile;

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.