Usage scenarios and considerations for modifiers in Java

Source: Internet
Author: User
Tags modifiers volatile

In the Java programming language, modifiers are modifiers that appear in the definition of a class, properties, methods, constructors, initialization blocks, and so on. Modifiers are divided into the access modifier and optional modifiers, they are basically keywords, there is an exception, is the access modifier in addition to public/protected/private there is a default access modifier, a total of 4. The optional modifiers are abstract/const/default/final/native/static/strictfp/synchronized/transient/volatile.

 

Modifier

Retouching scenes

Class

Member Methods

Construction method

Member variables

Local variables

Abstract (abstracted)

Static (statically)

Public (Common)

Protected (Protected)

Private (privately)

Synchronized (synchronous)

Native (local)

Transient (temporary)

Volatile (volatile)

Final (do not change)

No access modifier (default, same package access)

It is clear from the table above that the positions where the modifiers appear can be seen.

Const is a C-language keyword that restricts a variable from being allowed to be changed. The use of const can improve the security and reliability of the program to some extent. This keyword is reserved in Java, but the editor does not see its use in Java. In addition, when looking at other people's code, clearly understand the role of Const, to understand the other side of the program also has some help.

The following will parse the modifier by location:

1. class

In general, only the public/abstract/final and default modifiers are allowed, but with one exception, the static modifier is allowed in the inner class.

Public: Indicates that the class can be accessed by a class anywhere, by default (not written) indicating that the class is accessed by other classes in the same package.

Abstract: Indicates that the class is an abstraction class and cannot be instantiated. It should be noted that when the class is an abstract adornment, it is not required to have an abstraction method in it, and the class must be an abstract class when there is an abstract method in it.

Final: Indicates that the class is the final class and cannot be inherited (extended). You should be aware that when a class is final, you can still inherit other classes and implement interfaces.

Static: The outer class does not allow static adornments, but the inner class allows: class outter{static Class inner{}} can now produce an instance of an inner class directly from the class name of the outer class: New Outter.inner (); Instead of first generating an instance of an external class, Class outter{class inner{}},

Outter outter = new Outter ();
Outter.new Inner ();

When an inner class is not static, an inner class object can only be produced by an instance of the outer class.

2. member Variables

The declaration of a member variable of a class must be in the class body, not in a method, and a local variable is declared in the method.

The access modifier can be any of 4 types.

Static: Class variable: A variable owned by a class that is independent of the instance object of the class, regardless of how many objects the class creates, the system allocates memory for the class variable only the first time the class is called, and all objects share the class variable of that class, so the class variable can be accessed through the class itself (class name) or an object.

Final: Constant. A member variable can generally be undefined (assigning an initial value to a variable), but the final decorated member variable must be initialized at the time of definition, such as: final int age = 20; here the "= 20" cannot write, otherwise the compilation will error.

Volatile: Declares a variable that is controlled and modified by several threads that may be concurrently running.

Transient: Declares a variable that the value cannot be serialized (the variable still exists after the object is serialized, and the value is saved with the default value of that type, such as the integer type 0,string type is Null,boolean type is false).

3. Methods

The access modifier can be any of 4 types.

Abstract: Abstraction method: Only the declaration part, the method body is empty, specifically in the subclass to complete.

Static: Class method, static method:

1) call can be called directly using the class name, or an instance object name of the class can be called

2) cannot be owned by a single object, belonging to the entire class share.

3) member variables that cannot handle non-static adornments.

Final: A method that cannot be overridden by a quilt class.

Native: A local method that represents a special method written in another language, including C,c++,fortran, assembly language, and so on.

Synchronized: When a multithreaded call is synchronized, when this method is called, other threads are not able to call the method until the method returns, which is used to avoid inconsistent data results when multithreaded operations occur.

4. Local Variables

There can be only final adornments and no access modifier adornments.

5. Interface

The access modifier for an interface can only have public/default (same package) two, its optional modifier can only be abstract (abstract also can not write, the interface is abstract by default).

The variables in an interface can only be public static final union-decorated common statics (these three modifiers can not be written, the variables in the interface have these three modifiers by default, and the final type of the variable must be initialized at the time of definition (the initial value is assigned), which is already stated in the "variables" above)

The method access modifier in an interface can only be public, and in general it can be elective identifier abstract, which means that the method in the interface is the default abstract method and cannot have a method body.

In addition, there can be strictfp/static/default adornments (where static and default can only be selected, STRICTFP must be used with a mate in static and default)

Static method must have a method body.

Default: The modified method must have a method body.

Strictfp:strict Floatpoint Precision floating point

Note: The class-to-interface relationship is a multi-implementation, and the implementation class must override the method when the class implements multiple abstract methods with the same name, the same parameter, the same return type, or the default method (including two are abstract, two are default, and one is the default for the abstract).

When the method in the interface is a static method, the method can be called directly with the interface name, regardless of the implementation class. An implementation class can have the same static or non-static method.

6. Construction Method

You cannot have a return type before the method is constructed, but with one exception: You can have a void return type, which is generally not written by default, but it is not an error to write this void (no return value) adornment.

  

Usage scenarios and considerations for 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.