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
steven schwerin
7,722 PointsCrystal Ball App
What does it mean to initialize a variable as in the Pretty Little Things section?
6 Answers
Mike Bronner
16,395 PointsInitializing a variable means to create a container in the shape of the specified object you are creating on instance of. For example:
NSMutableString *myString = [[NSMutableString alloc] init];
Here I am specifying a new container called "myString" in the shape of the NSMutableString object. Then I am allocating it to memory, then initializing (preparing it for later use) with no contents. I now have a container I can use to store text in. :)
If your question is more specific to the crystal ball app, please specify the exact video and timestamp so that we can refer back to it. :)
Hope this helps a bit. ~Mike
J. T.
Courses Plus Student 582 Points public void animateFrog() {
ImageView frogImage = (ImageView) findViewById(R.id.frog);
....
AnimationDrawable jumpAnimation = (AnimationDrawable)frogImage.getDrawable();
}
If so:
AnimationDrawable jumpAnimation = (AnimationDrawable)frogImage.getDrawable();
You're creating an AnmationDrawable variable and it's being initialized to a AnimationDrawable object that points to the frogImage resource. getDrawable returns a Drawable object that points to the frogImage and the cast to AnimationDrawable says that we want the subclass object type that can work with the special things that differentiate AnimationDrawable from a regular Drawable, not the generic (not to be confused with generics - a different topic!) Drawable class type object. Look up inheritance to understand this subclass and casting stuff.
steven schwerin
7,722 PointsThank you so much for those answers. I'm still not processing most of what I am seeing, but this all helps! I think I need to take the programming sequence right after finishing the Crystal Ball App. As I learn more, I am going to come back to these explanations.
Joshua DeVille
Courses Plus Student 705 PointsI hear ya some things make sense others are still kind of cloudy.
Ben Jakuben
Treehouse TeacherJoshua DeVille post new questions in the Forum about anything that you want cleared up or to discuss further. :)
J. T.
Courses Plus Student 582 PointsSame as Mike. Here's the Java version as Mike posted an Objective-C (iOS) initialization.
String myString; // Variable called myString is declared but not initialized to some data or object. This declaration is saying "In the future, I will give some value or object that myString can hold"
String myString = "The variable is initialized when it points to some object or data"; // Variable holds a string of text and therefore it's considered initialized
steven schwerin
7,722 PointsI appreciate these answers. in general, i think I get the initialization concept now. My question came from the Animate the Crystal Ball challenge. The code that I didn't remember being explained had to do with initializing the crystal ball image with a .getDrawable() method (?). How does this initialize the variable or maybe, how will I know in the future when I need to initialize a variable?
I am tackling the Android version first as I don't have an iPhone as of yet...i will be getting one. I am not sure if that changes your answers or not.
Mike Bronner
16,395 PointsHi Steven, I'll have to bow out, as I have no Android experience. :)
steven schwerin
7,722 PointsThank you, Mike,
Your answer still applied. I like your analogy. This programming thing is blowing my mind at the moment. It looks like a lot of the fundamental analogies like you gave are going to come in handy as they apply across platforms. I will get to the iPhone though it might be a while.
steven schwerin
7,722 PointsYes! that is it! whew. confused at the moment. Why don't we just create an AnimationDrawable object and be done with it? is the point getting to the variable regardless of how we get there?
Ben Jakuben
Treehouse TeacherThanks for being an Android student! Let's talk about this code piece by piece.
ImageView frogImage = (ImageView) findViewById(R.id.frog);
Here we are creating an ImageView variable. We are setting it to the ImageView in our layout that has the ID 'frog'. In this case the variable is a reference we can use to access and manipulate the ImageView that we see in the display. We need it for the 2nd line...
AnimationDrawable jumpAnimation = (AnimationDrawable)frogImage.getDrawable();
First - the "why". In Android, some of our animations can be stored in variables. This allows us to use them in our app as well as programmatically change them. In this case we want a variable to hold the animation that will make the frog jump. In the following steps of the code challenge we will start and stop this animation.
So on the left side of the equals sign we are simply declaring the variable, which means we name it and tell the Android system what kind of variable it will be (an AnimationDrawable variable).
On the left side we are doing a few things. First, we are calling the getDrawable() method of the frogImage variable. This is a built-in Android method that gets us the 'drawable' part of the ImageView currently held in the frogImage variable. The 'drawable' part in this case is an animation sequence (not seen in this code).
But the getDrawable() method returns a generic Drawable value. This kind of value can be a plain image, an animation sequence, or other stuff. The method doesn't know what kind its getting - just that it falls under the umbrella of Drawable.
We know what we are getting, though. We know it's a specific type of Drawable called AnimationDrawable. In this kind of situation we do what is known as a cast. We cast a generic type of variable into a more specific type. So we cast from a generic Drawable to the more specific AnimationDrawable. AnimationDrawable is a subclass of Drawable.
The cast is performed by naming the subclass inside parenthesis.
Hope this helps explain! Keep on plowing through and come back and review if you need it. And keep posting good questions like this in the forum!
J. T.
Courses Plus Student 582 PointsIs it correct to think that we don't directly call the AnimationDrawable's constructor like below
frogImage = new AnimationDrawable();
because an ImageView reference variable can't directly hold an AnimationDrawable object so View (super class of ImageView) defines a getDrawable() method that allows View & its subclasses to take in a resource and return a generic Drawable object. We return a generic Drawable object because there are and may be future Drawable sublcasses and it's easier and flexible to return the superclass object to retain future usability of the getDrawable method?
Ben Jakuben
Treehouse TeacherYou're definitely on the right track, but I got a little confused in your explanation. I think you are basically correct, though. It's hard to explain programming concepts in writing, isn't it? :)
So your first assumption is correct: frogImage is declared as an ImageView variable. So it must hold ImageView data. AnimationDrawable is a different type, so we can't assign AnimationDrawable data to an ImageView variable.
Your other assumption is that getDrawable() returns a generic drawable because "there are and may be future Drawable subclasses". There are different reasons methods might return generic data types. It's a common programming pattern. It allows us to set any of the Drawable subclasses as the "Drawable" property of an ImageView. This is easier in that ImageView doesn't need to have a property for every possible Drawable subclass, and it's future proof because new subclasses in the future wouldn't need to be added as new properties of ImageView.
Hope this helps, and thanks for adding to this discussion!
Also, a quick side note: the AnimationDrawable() constructor doesn't do anything for us. So we just skip it and set it automatically with the data returned by the getDrawable() method.
J. T.
Courses Plus Student 582 PointsLOL, you are right that it's a bit difficult trying to translate programming concepts into writing!
Thanks a lot of elaborating on the topic and responding to my post. The explanation that you wrote above is exactly what I was thinking. :)
J. T.
Courses Plus Student 582 PointsI'm still learning about Android's API and OOP stuff and while I have an idea why we don't directly create an AnimationDrawable object, I don't want to unintentionally give out bad information. Hopefully a more experienced Android developer can chime in and explain more. :)