The difference between Java learning--public,private,protected and other modifiers

Source: Internet
Author: User
Tags modifiers volatile

Public, private, PROTECTE, default are the access rights that are used in Java to define the members, that is, without any permission modifiers before the members. Such as:
public class a{
void method () {};
}
Method belongs to the default permission.

These four modifiers have access to the following table:
-----------------------------------------------
Intra-class package other
Public allow allow allowed allowed
Protected allow allow not allowed
Default allow disallowed not allowed
Private allow not allow not allowed
-----------------------------------------------

The meaning of the access modifier in Java, public, private, PROTECTE, default, is explained:
Public (interface Access): The most restrictive modifier in the Java language, commonly referred to as "common". The classes, properties, and methods that are decorated are not only accessible across classes, but also across packages (package).
Private (You cannot access): The narrowest modifier for access restrictions in the Java language is generally referred to as "private". Classes, properties, and methods that are modified by them can only be accessed by objects of that class, their subclasses cannot be accessed, and cross-package access is not allowed.
Protected (inherited access): An access modifier between public and private, commonly referred to as a "protected form." Classes, properties, and methods that are modified by them can only be accessed by methods and subclasses of the class itself, even if the subclasses are accessible in different packages.
Default (Package access): That is, without any access modifiers, often referred to as the "Default access mode". In this mode, access is allowed only in the same package.
(In the Java language only ...) )

The Java language defines the 6 common modifiers for public, protected, private, abstract, static, and final
The word also defines 5 less commonly used modifiers, and the following is an introduction to these 11 Java modifiers:
1.public
Working with objects: classes, interfaces, members
Description: The class (interface, member) is accessible regardless of the package definition in which it is located
2.private
Working with objects: members
Description: A member can be accessed only in the class in which it is defined
3.static
Working with objects: classes, methods, fields, initialization functions
Description: The inner class known as Static is a top-level class, and it is irrelevant to the members of the containing class. Static methods
is a class method,
is a pointer to an instance of the owning class rather than the class. A static field is a class field, regardless of the class where the field is created.
How many instances, the word
Only one instance of a segment is pointed to the class to which it belongs, not to an instance of the class. The initialization function is executed when the class is loaded
Instead of creating a
Executes when the instance is instantiated.
4.final
Working with objects: classes, methods, fields, variables
Description: A class that is defined as final does not allow subclasses, cannot be overwritten (not used for dynamic queries), field values
Not allowed to be
Modify.
5.abstract
Working with objects: classes, interfaces, methods
Description: The class includes methods that are not implemented and cannot be instantiated. If it is an abstract method, the method body
is empty, the party
The implementation of the method is defined in the subclass, and the class containing an abstract method must be an abstract class
6.protected
Working with objects: members
Description: A member can only be accessed in the package that defines it, and if it is accessed in another package, the class that implements this method
Must be the
Subclass of the class to which the member belongs.
7.native
Working with objects: members
Description: Related to the operating platform, the definition does not define its method, and the implementation of the method is implemented by an external library.
8.strictfp
Working with objects: classes, methods
Description: All methods in the STRICTFP decorated class hide the strictfp modifier, and all floating-point methods executed by the method
Calculation compliance
IEEE 754 standard, all values including intermediate results must be expressed as a float or double type, and cannot be exploited
Floating on the local platform
Point format or hardware provides additional precision or range of representation.
9.synchronized
Working with objects: methods
Description: For a static method, the JVM locks the class it is in before execution, and for a non-static class
Law, Enforcement
Lock a particular object instance before the
10.volatile
Working with objects: Fields
Description: Because asynchronous threads can access fields, some optimizations do not necessarily work on fields.
Volatile sometimes
can replace synchronized.
11.transient
Working with objects: Fields
Description: The field is not part of the object's persistent state, and the fields and objects should not be strung together.

The difference between Java learning--public,private,protected and other modifiers

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.