Java modifier range

Source: Internet
Author: User

Class:

There are several modifiers for classes in Java: Private, Default (package), protect, public, and the following table:

Range Private Default Protected Public
The same class √ √ √ √
Classes in the same package √ √ √
Classes in the same package, subclasses in different packages √ √
All √

Add: There are final, and abstract modifiers:

The final decorated class cannot be inherited, and there are no subclasses.

An abstract-decorated class cannot be instantiated and must inherit from the quilt class. A class must be an abstract class as long as it has an abstract method, but an abstract class does not have to have an abstract method.

Variable:

1, there is no global variables in Java, as long as the method variables, instance variables (non-static variables in the Class), class variables (static variables in the class)

2, the variable in the method cannot have an access modifier.

3, class variable modifiers private, default (package), protect, public, and the scope of the class is the same, the following highlights several other:

modifiers
name Description Notes
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 a class to access static variables
Final constants, values can be assigned only once, and cannot be changed Be careful not to use const, although it can be used with static as the Const keyword in C and C + + to avoid maintaining a copy of each instance of the class
Transient Tells the compiler that this variable does not need to be persisted when the class object is serialized The main reason is that the amount of change can come through other variables, using it for performance problems
Volatile Indicates that multiple threads may modify this variable, requiring compiler optimizations to ensure that modifications to this variable are handled correctly

Interface:

Public is generally used.

Part excerpt from:

http://www.bokee.net/bloggermodule/blog_viewblog.do?id=110495

Method: The modifiers and variables of the method are almost the same.

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.