Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - static variable and parameter with same name

Tags:

java

Suppose I have a Helper class as below:

public class Helper {
    private Context context;
    private static HelperListener listener;


    public Helper(Context context, HelperListener listener) {
        this.context = context;
        listener = listener; // Can't tell which one
    }
}

context and listener are variables that will be set only once, in the constructor.

context is not static, hence I can differentiate the variable from the parameter using this.context.

listener, on the other hand, is static. Is there any way to differentiate it from the parameter when it comes to static variables?

like image 268
Guilherme Avatar asked Apr 01 '14 13:04

Guilherme


2 Answers

You can qualify the static variable with the class name to differentiate it:

Helper.listener = listener;
like image 107
arshajii Avatar answered Oct 27 '22 00:10

arshajii


You could use Helper.listener = listener; although setting the value of a static variable from a constructor is not recommended.

like image 26
anirudh Avatar answered Oct 26 '22 22:10

anirudh