Java modifier

Java modifier


Java language provides a lot of modifiers, mainly divided into the following two categories:
  • Access modifier
  • Non-access modifier
Modifiers are used to define classes, methods, or variables, usually at the top of the statement. We illustrate the following example:
public class className { // ... } Private boolean myFlag ; static final double weeks = 9 .5 ; protected static final int BOXWIDTH = 42 ; public static void main ( String [ ] arguments ) { // method body }

Access control modifier

In Java, access controls can be used to protect access to classes, variables, methods, and constructors. Java supports 4 different access rights.
By default, also known as default , visible in the same package, do not use any modifiers.
Private, specified as private modifier, visible in the same category.
Common, specified as public modifier, visible to all classes.
Protected , specified with the protected modifier, visible to classes and all subclasses within the same package.
We can use the following table to illustrate the access:
Access control
ModifierCurrent classThe same bagDescendantsOther packages
publicYYYY
protectedYYYN.
defaultYYN.N.
privateYN.N.N.

Default access modifier - do not use any keywords

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 method in the interface defaults to public by default.
As shown in the following example, declarations of variables and methods may not use any modifiers.

Examples

String version = " 1.5.1 " ; boolean processOrder ( ) { return true ; }

Private access modifier --private

Private access modifiers are the most restrictive access levels, so methods, variables , and constructors declared as private can only be accessed by their classes, and classes and interfaces can not be declared private .
Variables declared as private access types can only be accessed by external classes through the common getter method in the class.
The use of the Private access modifier is mainly used to hide the implementation details of the class and protect the data of the class.
The following classes use private access modifiers:
public class Logger { Private String the format ; public String getFormat ( ) { return the this . the format ; } public void the setFormat ( String the format ) { the this . the format = the format ; } }
In the example, the format variable in the Logger class is a private variable, so other classes can not get and set the value of the variable directly. In order for other classes to be able to manipulate the variable, two public methods are defined: getFormat () (return the value of format) and setFormat (String) (set the value of format)

Public access modifier - public

The classes, methods, constructors, and interfaces that are declared public can be accessed by any other class.
If several public classes are distributed in different packages, you need to import the corresponding public class package. Because of the inheritance of classes, all public methods and variables of a class can be inherited by their subclasses.
The following functions use public access control:
public static void main ( String [ ] arguments ) { // ... }
The main () method of the Java program must be set to public, otherwise the Java interpreter will not be able to run the class.

Protected access modifier --protected

Declared variables that are protected, methods, and constructors can be accessed by any other class in the same package, or can be accessed by subclasses in different packages.
Protected access modifiers can not be modified Classes and interfaces, methods and member variables can be declared as protected, but interface member and member methods can not be declared protected.
Subclasses can access the methods and variables declared by the protected modifier, which protects unrelated classes from using these methods and variables.
The following parent class uses the protected access modifier, which subclasses the openSpeaker () method of the parent class.
Class AudioPlayer { protected boolean openSpeaker ( Speaker sp ) { // implementation details } } Class StreamingAudioPlayer { boolean openSpeaker ( Speaker sp ) { // implementation details } }
If the openSpeaker () method is declared private, the class other than AudioPlayer will not be able to access the method. If openSpeaker () is declared public, then all classes can access the method. If we only want the method to be visible to the subclass of its class, the method is declared protected.

Access control and inheritance

Please note the following rules for inheritance:
  • The method declared in the parent class as public must also be public in the subclass.
  • The method declared in the parent class as protected is either declared as protected in the subclass or declared as public and can not be declared private.
  • The parent class declares a private method that can not be inherited.

Non-access modifier

In order to achieve some other features, Java also provides a number of non-access modifiers.
Static modifier used to create class methods and class variables.
Final modifier, used to modify classes, methods and variables, final modified class can not be inherited, modified method can not be re-defined by the inheritance class, modified variable is constant, is not modified.
Abstract modifier used to create abstract classes and abstract methods.
Synchronized and volatile modifiers, mainly used for thread programming.

Static modifier

  • Static variable:
    The static keyword is used to declare static variables that are independent of the object. Regardless of how many objects are instantiated by a class, it has only one copy of the static variable. Static variables are also called class variables. Local variables can not be declared as static variables.
  • Static method:
    The static keyword is used to declare a static method that is independent of the object. Static methods can not use nonclass static variables. The static method gets the data from the parameter list and then calculates the data.
Access to class variables and methods can be accessed directly using classname.variablename and classname.methodname .
As shown in the following example, the static modifier is used to create class methods and class variables.
public class InstanceCounter { Private static int the numInstances = 0 ; protected static int getCount ( ) { return the numInstances ; } Private static void addInstance ( ) { the numInstances ++; } InstanceCounter ( ) { InstanceCounter . addInstance ( ) ; } Public static void main ( String [ ] arguments ) { the System . OUT . The println ( " Starting with " + InstanceCounter . GetCount ( ) + " instances " ) ; for ( int I = 0 ; I < 500 ; ++ I ) { New InstanceCounter ( ) ; } The System . OUT . The println ( " the Created " + InstanceCounter . GetCount ( ) + " instances " ) ; } }
The above example runs the editing results as follows:
Started with 0 instances
 Created 500 instances   

Final modifier

Final variable
The final variable can be explicitly initialized and can only be initialized once. A reference to an object declared as final can not point to a different object. But the final object in the data can be changed. That is, the final object reference can not be changed, but the value inside can be changed.
The final modifier is usually used with the static modifier to create class constants.

Examples

Public class Test { final int value = 10 ; // The following is an instance of the declaration constant public static Final int BOXWIDTH = . 6 ; static Final String TITLE = " Manager " ; public void the ChangeValue ( ) { value = 12 is ; // outputs an error } }

Final method

The final method in a class can be inherited by a subclass, but can not be modified by a subclass.
Declaring the final purpose of the final method is to prevent the content of the method from being modified.
As shown below, use the final modifier to declare the method.
Public class Test { public final void changeName ( ) { // method body } }

Final class

The final class can not be inherited, and no class can inherit any of the properties of the final class.

Examples

Public final class Test { // class }

Abstract modifier

Abstract class:
An abstract class can not be used to instantiate an object, declaring that the only purpose of an abstract class is to extend the class in the future.
A class can not be modified by both abstract and final. If a class contains an abstract method, then the class must be declared as an abstract class, otherwise a compile error will occur.
Abstract classes can contain abstract methods and non-abstract methods.

Examples

Abstract class Caravan { private double price ; private String model ; private String year ; public abstract void goFast ( ) ; // abstract method Public abstract void changeColor ( ) ; }

Abstract method

An abstract method is a method that does not have any implementation, and the concrete implementation of the method is provided by subclasses.
The abstract method can not be declared as final and static.
Any subclass that inherits an abstract class must implement all abstract methods of the parent class unless the subclass is also an abstract class.
If a class contains several abstract methods, then the class must be declared as an abstract class. Abstract classes can not contain abstract methods.
Abstract method declarations end with a semicolon, for example: public abstract Sample (); .

Examples

Public abstract class SuperClass { abstract void m ( ) ; // abstract method } Class SubClass extends SuperClass { // Implement the abstract method Void m ( ) { ......... } }

Synchronized modifier

The synchronized keyword declaration method can only be accessed by a thread at the same time. The synchronized modifier can be applied to four access modifiers.

Examples

Public synchronized void showDetails ( ) { ....... }

Transient modifier

When the serialized object contains instance variables that are transiently modified, the java virtual machine (JVM) skips the particular variable.
The modifier is included in the statement that defines the variable and is used to preprocess the data type of the class and variable.

Examples

Public transient int limit = 55 ; // will not be persistent Public int b ; // persistence

Volatile modifier

Volatile-modified member variables are forced to re-read the value of the member variable from shared memory each time a thread is accessed. Also, when a member variable changes, the thread is forced to write the changed value back to the shared memory. So at any time, two different threads always see the same value for a member variable.
A volatile object reference may be null.

Examples

Public class MyRunnable implements Runnable { private volatile boolean active ; public void run ( ) { active = true ; while ( active ) // first line { // code } } Public void stop ( ) { active = false ; // second line } }
Normally, in a thread call the run () method (the Runnable open thread), the other thread calls the stop () method. If the first line of active value in the buffer is used, then the second row cycle does not stop when the active is false.
But the above code we used the volatile modification active, so the cycle will stop.

1 comments:


EmoticonEmoticon