Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Paritosh Sahni
PLUS
Paritosh Sahni
Courses Plus Student 4,853 Points

final keyword and Anonymous Inner Class (AIC)

Hello, So I already went through some discussions regarding final and AIC but I still have certain doubts and if somebody can help me with this then it would be great!

  1. Is it important to write the final keyword with a data type- variable even if I am just displaying the value or reading it but nowhere in the AIC changing the value?

I went through a blog and it says "In java 8, since we are only accessing the value in AIC, then the variable need not be declared "final". But when I am doing this in Android Studio, it is not allowing me to do so.

  1. Also, if I define the variable to be an instance variable (class) and change the value of instance variable in the AIC, will the changes persist outside the inner class?

Again, I went though the blog "http://blog.brendanlacanette.com/2015/08/variable-is-accessed-within-inner-class.html" and it says the changes will persist and when I tried this in Android Studio using Log, the value didn't change.

``` Android, Java

public class FunFactsActivity extends AppCompatActivity {

private TextView textView;
private Button showFactButton;
private RelativeLayout relativeLayout;
int a = 5;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fun_facts);

    textView = findViewById(R.id.factTextView);
    showFactButton = findViewById(R.id.showFactButton);
    relativeLayout = findViewById(R.id.mainLay);


    showFactButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            ChangeFact changeFact = new ChangeFact();
            String fact = changeFact.showFact();
            ChangeColor changeColor = new ChangeColor();
            int color = changeColor.color();
            textView.setText(fact);
            textView.setTextColor(color);
            showFactButton.setTextColor(color);
            relativeLayout.setBackgroundColor(color);
            Log.d("Value", a + "");
            a = 7;

        }
    });
    Log.d("New Val", a + "");

}

}

Probably, I am understanding and applying the blog wrong. Can someone please answer these questions for me so that I can have a better understanding? Thanks in advance! :)

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

When you log the "New" value it's really just the old value because you've only set the listener, not ran it. Setting a listener or handler doesn't run it's code right away. It runs the code at some time in the future when the specified event happens (in this case, when the user presses the button).

Paritosh Sahni
Paritosh Sahni
Courses Plus Student 4,853 Points

Hey Seth, Thank you for the immediate response. So I understood what you meant and tried what you suggested. Even after clicking the button and logging the "New Val", the value of a didn't change to 7 (a was 5 before and remained 5, which was changed to 7 in AIC).

Basically what you mean is that the instance variable values can be changed in the AIC and this change will reflect outside the inner class, correct?