Java modifiers, access control

Source: Internet
Author: User
Tags modifiers

Java modifier type (public,protected,private,friendly)

Public classes, generic variables and methods, and any class within and outside the package can be accessed;
Protected classes, generic variables and methods, any class within the package, and those outside the package that inherit from this class can be accessed;
Private class, generic variables and methods, any class outside the package can not be accessed;
If a class, a generic variable, and a method are not decorated with these three modifiers, it is the friendly type, then any class within the package can access it, and no class outside the package will be able to access it (including subclasses that inherit this class outside the package), so this class, generic variable, and method are friendly to other classes within the package. Open, while the other classes outside the package are closed.

class: access Modifiers modifier class class name extends Parent class name implement Interface Name(The location of the access modifier and the modifier can be interchanged)

Access modifiers
Name Description Note
Public can be accessed by all classes (using) The public class must be defined in a file with the same name as the class name
Package can be accessed by a class in the same package (using) The default access rights, which can be omitted from this keyword, can be defined in the same file as the public class

Modifier
Name Description Note
Final Classes that use this modifier cannot be inherited
Abstract If you want to use the abstract class, you must first build a new class that inherits the abstract class, and the new class implements the abstraction method in the abstract class. class must be defined as abstract, as long as there is an abstract method, but the abstract class is not necessarily protected by the abstract method

variablesThere are no global variables in Java, only method variables, instance variables (non-static variables in a class), class variables (static variables in a class). The variables in the L method cannot have access modifiers. So below access Modifier TableOnly for variables defined in the class. L When declaring an instance variable, if no initial value is assigned, it will be initialized to null (reference type) or 0, False (original type). The more complex instance variables can be initialized by an instance variable initializer, which is a block of statements with {}, which runs after the constructor of the class is invoked, before the constructor of the parent class constructor. The L class variable (static variable) can also be initialized by a class-variable initializer, which is a block of statements contained in static{} and may only be initialized once.

access Modifier
name Description remarks
public can be accessed by any class  
protected can be used by all classes in the same package Access can be accessed by all subclasses subclasses are not also accessible in the same package
privat E  
default no access modifier can be accessed by all classes in the same package If the subclass is not in the same package, you cannot access the

Modifier
Name Description Note
Static Static variables (also called class variables, others are called instance variables) Can be shared by all instances of the class. You do not need to create an instance of the class to access the static variable
Final Constant, the value can only be assigned once and cannot be changed Be careful not to use const, although it can be used in conjunction with static as the Const keyword in C, C + +, to avoid maintaining a copy of each instance of the class
Transient Tells the compiler that this variable does not need to persist when the class object is serialized Mainly because the amount of change can be obtained by other variables, it is used for performance problems
Volatile Indicates that there may be multiple threads modifying this variable, requiring compiler optimizations to ensure that modifications to this variable are handled correctly

Method access Modifiers modifier return type Method Name ( parameter list ) throws List of violationsThe constructor method of the L class is not able to have modifiers, return types, and constructor methods of the throws clause L class when it is called, it first calls the constructor method of the parent class, and then runs the initializer for the instance variable and the static variable before the constructor itself is run. L If the constructor method does not display a constructor that calls a parent class, the compiler automatically adds a default super () to it, and if the parent does not have a default parameterless constructor, the compiler will error. Super must be the first clause of a constructor method. L NOTE the use of the private constructor method.

access Modifier
name Description remarks
public You can access  
protected can be used by all classes in the same package Access can be accessed by all subclasses subclasses are not also accessible in the same package
privat E  
default no access modifier can be accessed by all classes in the same package If the subclass is not in the same package, you cannot access the

Modifier
Name Description Note
Static Static methods (also known as class methods, others called instance methods) Providing a service that does not depend on a class instance does not require that an instance of the class be created to access the static method
Final Prevent any subclasses from overloading the method Be careful not to use const, although it can be used in conjunction with static as the Const keyword in C, C + +, to avoid maintaining a copy of each instance of the class
Abstract Abstract methods, methods that have been declared in a class and not implemented You cannot declare a static method, a final method, or a constructor method of a class as abstract
Native The method defined with this modifier is not implemented in the class, and in most cases the implementation of the method is written in C and C + +. See Sun's Java Native Interface (JNI), which provides the runtime to load an implementation of a native method and associate it with a Java class function
Synchronized Multi-threaded support When one of these methods is called, no other thread can call the method, and the other synchronized method cannot call the method until the method returns

Interface access Modifiers Interface Interface Name extends Interface ListThe L interface cannot define any implementations of its declared methods the variables in the L interface always need to be defined as "public static final interface name", but can not contain these modifiers, the compiler defaults to this, the display contains modifiers mainly for the program clear

Access modifiers
Name Description Note
Public All
No access modifier (default) In the same package

Java modifiers, access control

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.