Define a class
package t1;
public class T1 {
private int a = 10;/ / private
int b = 20;/ / the default
protected int c = 30;// Protected
public int d = 40;/ / public
public void name(a) { System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); }}Copy the code
Test classes under the same package
package t1;
public class T2 {
public static void main(String[] args) {
T1 t1 = new T1();
//System.out.println(t1.a); Variables decorated with private are not available in other classes in the same packageSystem.out.println(t1.b); System.out.println(t1.c); System.out.println(t1.d); }}Copy the code
Test classes under different packages
package t2;
import t1.T1;
public class Test1 {
public static void main(String[] args) {
T1 t1 = new T1();
// System.out.println(t1.a); // The output class is not available
// System.out.println(t1.b); // By default, the same package can be accessed
// System.out.println(t1.c); // Protected variables are not accessible or inherited under different packages
System.out.println(t1.d);// Use any variable that is modified by public}}Copy the code