How can I access the main object of a class from a function inside a class in Java?

Read 2 comments in the following code.

public class Field extends LinearLayout { public void init() { setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { // I want to access the main object 'Field' here(not the class, the object) } }); // to be clear the object referred as 'this' from HERE should be accessed from where the above comment is. } } 

Is it possible? Is there a keyword for accessing the main class object from a function inside the object?

+5
source share
1 answer

Yes, you must use Field.this to access a Field instance from an instance of an anonymous class.

+6
source

Source: https://habr.com/ru/post/1242973/


All Articles