Reputation: 599
I'm new to Android development and so far I have been mostly figuring out how to get tutorial code to work in an app I'm building. I am struggling in understanding where to declare variables and why I copy and paste some tutorials that end up with errors.
For example, I am trying to learn how to use the ViewFlipper. From this tutorial I have issues with Eclipse telling me that vf is not a variable when I use it inside OnClick. So when I move the line:
ViewFlipper vf = (ViewFlipper)findViewById(R.id.viewFlipper1);
to just above my OnCreate method, then it works. Why do I see so many tutorials with the variables declared in OnCreate and why doesn't it work for me? Where is the appropriate place to declare them? I understand the basics of encapsulation and inheritance, so is OnCreate just like any other method and any variables declared in there are isolated from other methods? What about my buttons, should I declare those inside my class but outside the OnCreate?
Upvotes: 1
Views: 2101
Reputation: 4877
Why do I see so many tutorials with the variables declared in
OnCreate
and why doesn't it work for me?
Declaring variables within a method, or, outside, really boils down to what kind of scope
you want to associate with the variable.
Inside a method, you need to be careful that you're declaring a variable before you intend to use it.
Where is the appropriate place to declare them?
Appropriateness of where to declare a variable depends entirely on where all you want to use it. Declaring a variable at the class level, when you only intend to use it within a method is unwise. And declaring variables within methods, when a lot many other methods in the class want to access it, and you end up passing the variables to each method - is also unwise. So for now, you can just consider:
I'd like to add, that 1 and 2 are not universal rules that can be applied blindly - but to get off to a start, you can follow them, until you figure out the deeper nuances associated with variable scoping, access specification and lifetime.
I'm not talking about access specifiers here, since, they merit a much detailed understanding, which you can get here. And neither have i talked about the difference of instance vs. class variables here, because you'd be better of referring to an official doc like this one.
I understand the basics of encapsulation and inheritance, so is
onCreate
just like any other method and any variables declared in there are isolated from other methods?
That's correct.
What about my buttons, should I declare those inside my class but outside the
onCreate
?
UI components in Android are typically required by multiple methods to access. TextView
, Button
, etc - usually have states which need to be altered at different times, from different methods - so you'd be better off declaring them at the class level.
Another important reason why you should prefer declaring UI variables at the class level is that you reduce the overhead of having the Android framework create them for every method invocation. As long as your Activity
instance is alive, the framework holds onto the UI components/variables.
Upvotes: 4
Reputation: 724
this is because outside the onCreate method, variables are global to your class. Aka, any method in your class can access those variables. When you declare variables within onCreate, those variables are only accessible within the brackets of the oncreate method. You should declare your variable outside the onCreate method, and initialize them within the onCreate method:
ViewFlipper vf;
onCreate(....)
vf = (ViewFlipper) findViewById(R.id.viewFlipper1);
then set your onClickListener
Upvotes: 0
Reputation: 746
onCreate is just like any other method and all the java rules regarding encapsulation and variable scope apply to it. Declare the ViewFlipper variable as a class member, outside your onCreate method. But instantiate it inside your onCreate method, just as the tutorial says.
Everything in android works just as it does in java. All the programming rules of java are followed here.
Upvotes: 0