English subtitles

← 13b s What are Java Objects

Get Embed Code
13 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. Let's do the first one,
    it says TextView = new TextView, and
  2. then we pass context in as an input
    parameter to the constructor.
  3. The problem with this line of code is
    that we're creating a new object but
  4. we're not storing it
    properly in a variable.
  5. We're saying that the variable
    should have data type TextView but
  6. we forget to give it a variable name.
  7. So the correct code would
    look something like this.
  8. TextView followed by a variable name and
    then equals new TextView.
  9. You can call the variable
    name something else but
  10. we just chose to use textView
    with a lower case t.
  11. So in this case,
    the error was missing variable name.
  12. Let's do the next one.
  13. It says, image view img = new ImageView.
  14. The problem here is that this
    is not a valid object data type.
  15. Android defines a class called ImageView
    with a capital I and a capital V and
  16. it's squished together into one word.
  17. A valid class name can't
    have any spaces in it.
  18. The variable name looks okay, and
  19. then creating the new image
    view looks okay as well.
  20. So the only error was that it
    had an incorrect class name.
  21. In the third statement, we're trying
    to create a new toggle button.
  22. It says ToggleButton button
    = create ToggleButton.
  23. We're calling the ToggleButton
    class here, so
  24. we need to use the Java keyword new,
    not create.
  25. And this is the correct code.
  26. It says ToggleButton
    button = new ToggleButton.
  27. The problem here was not
    using the Java keyword new.
  28. New is the keyword that indicates that
    we're trying to create a new object.
  29. In the fourth statement,
    we're trying to create a new toast.
  30. You could've Google searched for
    how to create a new toast and
  31. then compared it with this code here.
  32. The problem here is that toast needs
    to be spelled with a capital T,
  33. because this is a class name for Toast.
  34. This is using a factory method
    like we talked about earlier.
  35. This creates a new toast object and
    then it gets stored in this variable.
  36. So the error here was just
    an incorrect class name.
  37. So now that we know how to create
    objects, how do we use these objects?