Modifiers in Java

Source: Internet
Author: User
Tags instance method modifiers

In Java, modifiers are divided into access-rights modifiers and non-access modifiers. The Java language elements that can be decorated with modifiers are classes, variables, methods, and interfaces. The following describes the use of modifiers on these four elements, respectively.

Class

The format of the declared class in Java is:

access Modifiers Modifier class class Name extends Parent class name implements interface Name {}

where access modifiers and modifiers can be interchanged, the access modifiers that can be applied to the class are public and package,public indicate that the class can be used by any class. The package represents the packet access permission, which is the default access permission, which can be omitted, and a package modifier is used to indicate that the class can be accessed by all classes in the same packet. Also, once the public modifier is used in the declaration of the class, the source file name must be the same as the class name of the public class. There can be only one public class in a Java source file.

You can make the non-access control modifiers for the class final and abstract. Final indicates that the class cannot be inherited (the class cannot be inherited when the constructor method of the class is private.) Because the constructor of the parent class is called when the subclass is instantiated, even if no call to Super () is displayed, the compiler will use Super () by default to invoke the constructor of the parent class, and if the constructor method of the parent class is set to private, it cannot be accessed by the class, so the subclass cannot be instantiated. In addition, if the subclass is instantiated as a super that the compiler calls by default, the parameterless constructor is called, and if the parent class does not have a parameterless constructor, an error is given. Abstract indicates that a class is an abstract class that cannot be instantiated by an abstract class and is used as an inherited base class. In another class, the class must be declared abstract, as long as there are functions that are abstract-modified.

Variable

There are no global variables in Java, only method variables (local variables), instance variables (non-static variables in a class), and class variables (static variables in a class). Where method variables cannot be decorated by modifiers, the variables discussed here are variables defined in the class. When a class is instantiated, the variables in the class are initialized by default to null (reference type), 0 and False (base type) if they are not initialized. Local variables are not initialized, they need to be initialized manually, and if not initialized, variables are not allowed if the compiler uses a local variable without initialization to give an error. Alternatively, you can use the initializer to initialize the variables in the class into static variable initializers and instance variable initializers, respectively, using static{} and {} definitions. A static variable initializer is used only once, when the first instance of a class is constructed. In addition, the initializer executes before the constructor, and the static initializer runs before the instance variable initializer. The modifiers that can be used for variables are discussed below.

First, the access control modifiers, including the public,private,protected, and the default control modifiers. Public means that it can be accessed by all classes, private means it can only be accessed in the class, protected means that it is accessible to classes in the same package, or it can be accessed by subclasses in different packages, and the default access rights represent only the classes in the same package. Even subclasses in different packages cannot be accessed.

There are four types of non-access modifiers that can modify variables, namely final,static,volatile and transient. Where final represents a constant, the final modified variable becomes constant, and the value can only be assigned once, and then it cannot be changed. The final variable can be initialized at the time of definition, or it can be assigned to a constructor or initializer, and once the object construction is finished, the value of the final variable cannot be changed. (in addition, static variables can only be initialized in the static initializer.) Static indicates that a variable is a class variable, that a class variable is not part of any class instance, that it is a variable of a class, and that all instances of the same class share a static variable. Transient tells the compiler not to persist this variable when the class is serialized, because the value of the variable can be obtained using a different value. Volatile indicates that there may be multiple threads modifying this variable, so compiler optimizations are required to ensure that modifications to this variable are handled correctly.

Method

The method is defined as follows:

access Modifiers modifier return type Method Name (parameter list) throws List of violations

Methods in Java are similar to variables, there is no global method, and methods in Java are defined or declared in the class or interface. In addition, the constructor method in the class is special, it cannot be decorated with ordinary modifiers (access modifiers can be used), there is no return type, and cannot have a throw clause, and must be the same as the class name. When the constructor method is called, the constructor of its parent class is called first, and then the initializer for the static variable and the instance variable is run, and the last constructor method is executed. If the display calls the parent class constructor, then super should be the first statement of the constructor.

The access modifier for a decorated method is basically the same as the access modifier for a decorated variable, and you can look at the following table:

access modifier

remarks

< P align= "left" >public

&NBSP;

protected

can be accessed by all subclasses

private

&NBSP;

default

can be accessed by all classes in the same package

As you can see, there is no difference between these provisions and the access modifiers of the modifier variables.

There are 5 non-access modifiers for the adornment method, namely final,static,abstract,native and synchronized. Final describes a method that cannot be overridden, that is, the subclass can inherit the final method of the parent class, but cannot override this method. Also, in some cases, it is possible for the compiler to treat a method that uses the final adornment as an inline (inline) function. That is, call this method without using the normal function call mechanism, that is, the stack pressure stack and other traditional methods of calling functions. Instead, the body of the function is inserted at the call. Doing so eliminates the overhead of a function call, but can cause problems such as code bloat, so whether it is handled as an inline function requires a compiler's judgment. Static indicates that the modified method is a static method, not an instance method. Instance methods of a class can call static methods and static variables, but static methods cannot invoke non-static methods and instance variables, because static methods are methods shared by all instances, and static method calls to non-static methods cannot determine which instance is being processed. Abstract indicates that a method is an abstract method that declares but does not define a method. A class that has an abstract method must be an abstract class. Native-modified methods are not defined in the class, and are generally implemented by C + +. (JNI mechanism.) Synchronized means that for multi-threaded support, when a method is modified by it, if the method is called in a thread, it cannot be called in another thread until the method returns.

Interface

The interface is defined in the following form:

access Modifiers Interface Interface Name extends Interface List

An interface cannot define any implementation of the method it declares, and the variables in the interface must always be defined as public static final, which is not the case with the default. The access modifier for an interface has pulic and the default access modifier, which is the package modifier.

Modifiers in Java

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.