The scope scope of Java refers to the range of the variable can be effective, the variables declared in different places have different scopes, and the scope of the decision is the position of curly braces, but also determines the visibility of the variable name and life cycle.
JavaIn language, the types of variables are mainly member variables, static variables, and local variables three. The scope of a member variable of a class is the same as the scope of the class object, and when the class is instantiated, the member variable allocates space in memory and initializes until the end of the life cycle of the instantiated object.
staticThe modified member variable is a static variable or a global variable, and unlike a member variable, a static variable is not dependent on a particular instance, but is shared by all instances, so long as a class is loaded the JVM allocates memory space for the static variables of the class, so Static variables can be accessed through the class name and variable name. The local variable is inside the method, and its scope and visibility are within the curly braces in which he is located. Four scopes for member variables
| scope, Visibility |
Current Class |
Same | Package
sub-class |
Other | Package
public |
Visible |
Visible |
Visible |
Visible |
private |
Visible |
Not visible |
Not visible |
Not visible |
protected |
Visible |
Visible |
Visible |
Not visible |
default |
Visible |
Visible |
Not visible |
Not visible |
Scope Explanation:
- public : Indicates that the member variable or method is visible to all classes or objects, and that all classes or objects can be accessed directly
- private : Indicates that the member variable or method is private and that only the current class has access to it, except for other classes or objects that do not have access rights. Subclasses also do not have access rights.
- protected : Indicates that the member variable or method is visible to the class itself, with other classes in the same package, and that the class under the other package is inaccessible unless it is his subclass
- default : Indicates that the member variable or method is visible only to itself and within the same package, and that the class within the other package cannot be accessed, even if its subclasses
It is important to note that these modifiers can only be used to decorate member variables and not be used to decorate local variables and
privateAnd
protectedcannot be used for decorated classes only
public , abstract , finalCan be used to modify the class instance title: The following statement is correct (); A. Instance methods you can call instance methods of a superclass directly B. instance methods can call class methods of a superclass directly C. Instance methods can directly invoke instance methods of other Classes D. Instance methods can be used to directly call the class method of this class for correct answer selection
D. Subclasses in the parent class are
priavateAnd
defaultType cannot access a method of the parent class, and only static methods can be called directly, so
DRight.
Scope of Java