Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can member variable and local method variable have the same name?

How would I accomplish this?

class Test {
    private int var1;

    public Test(int var1) {
        var1 = var1; //set the member variable to what was passed in
    }
}

I'm sure there's a very obvious answer. It's just escaping me right now.

like image 831
Greg Avatar asked Jan 27 '11 02:01

Greg


1 Answers

Yes, they can share the same name. However, to reference the instance variable, you need to use the this prefix:

public Test(int var1) {
    this.var1 = var1;
}
like image 94
Jonathon Faust Avatar answered Oct 11 '22 14:10

Jonathon Faust