2

I am a java beginner. My test code like this:

public class Test {
    private String a;
    private String b;

    public Test(String a, String b){
        this.a = a;
        this.b = b;
    }

    public Test(Test another){
        this.a = another.a;//My question comes from here
        this.b = another.b;
    }
    public String getA(){
        return a;
    }

    public String getB(){
        return b;
    }
}

My question is why Test another can access the private variable directly using ., rather than using the getA() or getB() method.

The tutorial tells me that private variable can not be accessed directly by .

Gemsnail
  • 23
  • 2

1 Answers1

0

Private variables cannot be accessed by a different class, but here you are in the same class.

rabz100
  • 751
  • 1
  • 5
  • 13