-
Title:
13b s What are Java Objects
-
Description:
-
Let's do the first one,
it says TextView = new TextView, and
-
then we pass context in as an input
parameter to the constructor.
-
The problem with this line of code is
that we're creating a new object but
-
we're not storing it
properly in a variable.
-
We're saying that the variable
should have data type TextView but
-
we forget to give it a variable name.
-
So the correct code would
look something like this.
-
TextView followed by a variable name and
then equals new TextView.
-
You can call the variable
name something else but
-
we just chose to use textView
with a lower case t.
-
So in this case,
the error was missing variable name.
-
Let's do the next one.
-
It says, image view img = new ImageView.
-
The problem here is that this
is not a valid object data type.
-
Android defines a class called ImageView
with a capital I and a capital V and
-
it's squished together into one word.
-
A valid class name can't
have any spaces in it.
-
The variable name looks okay, and
-
then creating the new image
view looks okay as well.
-
So the only error was that it
had an incorrect class name.
-
In the third statement, we're trying
to create a new toggle button.
-
It says ToggleButton button
= create ToggleButton.
-
We're calling the ToggleButton
class here, so
-
we need to use the Java keyword new,
not create.
-
And this is the correct code.
-
It says ToggleButton
button = new ToggleButton.
-
The problem here was not
using the Java keyword new.
-
New is the keyword that indicates that
we're trying to create a new object.
-
In the fourth statement,
we're trying to create a new toast.
-
You could've Google searched for
how to create a new toast and
-
then compared it with this code here.
-
The problem here is that toast needs
to be spelled with a capital T,
-
because this is a class name for Toast.
-
This is using a factory method
like we talked about earlier.
-
This creates a new toast object and
then it gets stored in this variable.
-
So the error here was just
an incorrect class name.
-
So now that we know how to create
objects, how do we use these objects?