[java] What is the difference between public, protected, package-private and private in Java?

____________________________________________________________________
                | highest precedence <---------> lowest precedence
*———————————————+———————————————+———————————+———————————————+———————
 \ xCanBeSeenBy | this          | any class | this subclass | any
  \__________   | class         | in same   | in another    | class
             \  | nonsubbed     | package   | package       |    
Modifier of x \ |               |           |               |       
————————————————*———————————————+———————————+———————————————+———————
public          |       ?       |     ?     |       ?       |   ?  
————————————————+———————————————+———————————+———————————————+———————
protected       |       ?       |     ?     |       ?       |   ?   
————————————————+———————————————+———————————+———————————————+———————
package-private |               |           |               |
(no modifier)   |       ?       |     ?     |       ?       |   ?   
————————————————+———————————————+———————————+———————————————+———————
private         |       ?       |     ?     |       ?       |   ?    
____________________________________________________________________

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to private

Private class declaration Python read-only property What is the use of a private static variable in Java? How to access private data members outside the class without making "friend"s? JUnit Testing private variables? What are access specifiers? Should I inherit with private, protected or public? Do subclasses inherit private fields? What is the difference between public, private, and protected? Internal vs. Private Access Modifiers Private Variables and Methods in Python

Examples related to public

How to declare Global Variables in Excel VBA to be visible across the Workbook C# Foreach statement does not contain public definition for GetEnumerator Call-time pass-by-reference has been removed Is it possible to declare a public variable in vba and assign a default value? What is the difference between public, private, and protected? Setting public class variables What is the difference between public, protected, package-private and private in Java?

Examples related to protected

What are access specifiers? Should I inherit with private, protected or public? What is the difference between public, private, and protected? What is the difference between private and protected members of C++ classes? What is the difference between public, protected, package-private and private in Java?

Examples related to access-modifiers

What is the default access modifier in Java? What is the difference between Dim, Global, Public, and Private as Modular Field Access Modifiers? Internal vs. Private Access Modifiers Class is inaccessible due to its protection level What are the default access modifiers in C#? What is the equivalent of Java's final in C#? What are public, private and protected in object oriented programming? In C#, what is the difference between public, private, protected, and having no access modifier? What is the difference between 'protected' and 'protected internal'? What is the difference between public, protected, package-private and private in Java?