Java Basics Learning: member variables: public, protected, default, private Scope

  1. The following are members of the variable scope contrast.
  2. The following modifiers can only modify member variables, local variables can not be modified. Because local variables inside the method itself can only be used so adding a modifier does not make sense.
  3. private and protected can not be modified outside the class, only the public, final, abstract can be modified outside of class
Scope and Visibility The current class The same package Subclass Other package
public
protected ×
default × ×
private × × ×

public: indicates that the member variable or method for all classes or objects are visible, all classes or objects can be accessed directly.
protected: it indicates that the class member variable or method itself, with its other classes, sub-classes in the same package the class in the other packages are visible.
default: indicates that the member variable or method only himself and his kind located in the same package visible. If the parent class and subclass in the same package, the sub-class of default member variables or methods of the parent class has access; if the father and son are in different classes within the package, there is no access.
private: indicates that member variable or method is private, only the current local access to its class, in addition to other classes (including subclasses) or object does not have access.

Published 33 original articles · won praise 5 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_41623154/article/details/105383400