0

I have two public classes class A and B.

public class A{
protected int x;

}

public class B{
A a = new A();
a.x // still possible even though its protected

}

In class A I define a protected variable, but I can still access this variable in B. Why, when it's protected?

I heard it is because they are in the same package, but this makes me confused. I didn't even create a package yet so how does protected work in this case? Are the two classes of the same non-existent package if I don't create packages for them to be in?

I tried outputting in both classes with

this.getClass().getPackage()

and it both gives me "null". So I don't understand what is going on here. are they in the same package or not?

user5846939
  • 293
  • 2
  • 11

0 Answers0