Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

Java Variables: Using variables between classes

Hello

I'm new to Java and I'm really struggling with the way variables work in this language.

I'm declaring my variables in the onCreate(Bundle savedInstanceState) class, but then I want to update the variables using a getClickTime.setOnClickListener(new View.OnClickListener() {} class.

But I can't do it. The variables are either set to final or they are not, but either way I'm getting one of these errors:

the final local variable cannot be assigned since it is defined in an enclosing type cannot refer to a non-final variable inside an inner class defined in a different method

I'm happy to do some reading to get my head around how I should be approaching this, but I can't find what to read. I was hoping there would be a resource within Treehouse that would explain these concepts but I can't find it. Please can someone point me in the right direction?

Thanks in advance

Dicky

8 Answers

Declare your variables before onCreate.

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

@Ernest - thanks for posting the quick solution!

@Dicky - it sounds like you were having problems due to "scope" issues with your variables. For what it's worth, here's a quick example/explanation of scope:

public class TestActivity extends Activity {
    protected String mFirstName = "Ben"; // This is available anywhere in this class

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        String lastName = "Jakuben"; // This is available only within this method
        Button testButton = (Button) findViewById(R.id.button1);
        testButton.setOnClickListener(new View.OnClickListener() {
            /* This is the tricky one. Because we declare this with the 'new' keyword,
             * this is what is known as an 'anonymous inner class'.
             * It's scope is all its own and is limited. Because it is its own thing that's actually 
             * separate from the 'onCreate()' method, it has to maintain its own separate
             * copies of variables. As its own thing it could potentially be around after the 
             * 'onCreate()' method is done and potentially destroyed by Java garbage collection.
             */
            @Override
            public void onClick(View v) {
                String middleName = "Android";
            }
        });
    }
}

Anonymous inner class

And another forum post that briefly talks about the final keyword

By defining your variables at the class level (per @Ernest's solution) you are guaranteeing that they'll be available whenever your anonymous inner class is used since the anonymous inner class is contained within the regular (outer) class, and thus its own lifecycle is tied to the outer class.

Is it the case that I need to set my sub class up so that variables are passed between it through methods?

Hi Ernest

Thanks for your reply. I'll give that a try.

Thanks

That works. Thanks very much

That's fantastic, Ben, thanks so much for your help. And thanks for the great videos.

I'm now on to new and exciting problems with my app: After extra development, it's not working at all ! So now I need to track down some debugging advice. searches frantically

Thanks

Dicky

I'm seven years late to the party but this information has helped me immensely. Thanks all! This was exactly my question.