YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English (United States) subtitles

← What Are Java Objects?

Get Embed Code
15 Languages

Showing Revision 4 created 09/24/2016 by Udacity Robot.

  1. Do you remember the boxes?
  2. >> What boxes?
  3. >> These boxes.
  4. >> Huh!
    >> You do remember them?
  5. >> Oh yeah, yeah.
  6. I do remember them.
  7. They're variables,
    where you can put the value inside.
  8. Yeah, and
  9. you can combine multiple of these
    variable to create a larger Java object.
  10. >> Oh, so you can take two or
    more variable and
  11. make into a something bigger.
  12. >> Exactly.
    >> Okay
  13. >> [SOUND] Like a TextView.
  14. >> Yeah.
    So, we can have an integer variable.
  15. >> This is for the font size.
  16. >> Exactly.
  17. >> Okay.
    [SOUND]
  18. >> And then you can also have
  19. a string variable.
  20. >> And the string variable for
    the actual text itself.
  21. >> Yeah, and those can go inside the
    TextView as the state of the TextView.
  22. >> And then we can interact with those
    things using different methods that
  23. we're going to play with next.
  24. >> Exactly.
  25. >> Okay.
    Let's do that.
  26. >> As we mentioned earlier, showing
    a TextView on the screen like this
  27. is actually controlled by
    a Java object within the app.
  28. This is an example of
    a TextView Java object.
  29. It's like a box, and
    inside of it, it holds state.
  30. The state is made up
    of a bunch of fields.
  31. A field is a variable,
    like in lesson two,
  32. when we learned about global variables.
  33. We can have a text field
    to store the today string,
  34. which is going to be the text that's
    ultimately displayed on the screen.
  35. We can also have an integer field to
    store the font size of this text.
  36. There's also a bunch of other fields,
  37. like text color, font family that
    make up the state of the TextView.
  38. I just didn't draw them in this picture.
  39. To interact with this Java object and
  40. change it while the app is running,
    we can call methods on the TextView.
  41. We learned about methods
    earlier in lesson three.
  42. Some example methods on a TextView
    are setText and getText.
  43. You can also have methods like
    setTextSize or getTextSize, and so on.
  44. Each of these methods just
    executes a list of instructions.
  45. You can have more complex
    methods like onDraw,
  46. which handles drawing
    a TextView onto the screen.
  47. So overall, this TextView object holds
    state information on what this TextView
  48. contains, and you can call methods
    to interact with this TextView,
  49. such as to modify the state,
    or to do more complex actions.
  50. To show an ImageView on the screen,
    we actually have a Java object for
  51. ImageView inside of the app.
  52. This particular ImageView contains state
    information, like which image should
  53. be shown, as well as the scale type,
    like center crop.
  54. Again there's a bunch of other fields
    that make up the state of an ImageView,
  55. I just didn't draw them here.
  56. There's methods like setImage,
    setScaleType, and even more complex
  57. methods, like onDraw, to handle drawing
    this ImageView onto the screen.
  58. Note that, as developers,
  59. we don't explicitly have to call onDraw
    to tell it to draw onto the screen.
  60. Android handles calling this
    automatically for us, but it is useful
  61. to be able to call these methods to
    change the ImageView, for example.
  62. So how do we know what all the fields
    are inside the state of an ImageView,
  63. and what all the methods
    are of this ImageView?
  64. To answer that question we have to look
    at the class definition for that object.
  65. It's a file called TextView.java,
  66. similar to how we defined
    MainActivity.java.
  67. Inside this class TextView file,
    we have a bunch of code that
  68. talks about the state of
    the TextView as well as the methods.
  69. Now it doesn't say state and
    methods exactly, but it just contains
  70. information on the state and
    it contains information on the methods.
  71. You can thank of this as defining
    the rules of how a TextView behaves.
  72. This object instance can hold specific
    information like the text string today.
  73. Or the text size 18.
  74. You can also use this class to create
    even more TextView object instances,
  75. and these could have
    a different text string value.
  76. I think this one could
    hold text that says hello,
  77. and this one could hold
    text that says goodbye.
  78. They could have different font sizes,
    different text colors, etc.
  79. However, they're all based
    off of the class template.
  80. You may find it hard to distinguish
    between a class and an object instance,
  81. but that's okay.
  82. It takes some time to get used to.
  83. Here's an analogy to help you.
  84. You can think of it as creating
    a floor plan for a house.
  85. You can use this floor plan
    to create the actual homes.
  86. Each of these homes can
    be slightly different.
  87. But they're all based off
    of this base floor plan.
  88. It's important to note that the floor
    plan is not a house itself.
  89. It's just the plan.
  90. Similarly, the TextView class is used
    to create these object instances.
  91. So these instances are the ones
    with the actual text values,
  92. text color values, and text sizes.
  93. So what does one of
    these classes look like?