Return to Video

What Are Java Objects? Pt. 2

  • 0:00 - 0:04
    We've created a simplified
    version of the TextView class,
  • 0:04 - 0:07
    which would be saved in
    the TextView.java file.
  • 0:07 - 0:08
    We simplified it so
    it would be easier for
  • 0:08 - 0:11
    you to understand what
    a class would look like.
  • 0:11 - 0:14
    First it starts with an access modifier.
  • 0:14 - 0:15
    It's marked as public, so
  • 0:15 - 0:19
    that other things like an activity
    can use this TextView class.
  • 0:19 - 0:21
    Then we have the word class.
  • 0:21 - 0:24
    This indicates to Java,
    that this is a class definition.
  • 0:24 - 0:26
    Then we have the name of the class.
  • 0:26 - 0:29
    The name of a class in Java
    starts with a capital letter and
  • 0:29 - 0:34
    it's in CamelCase, so there's no spaces
    in the capitalized subsequent words.
  • 0:34 - 0:37
    There's so other text here, but
    we'll look into that later.
  • 0:37 - 0:39
    Important thing is,
    we have a opening brace and
  • 0:39 - 0:42
    then at the bottom of the definition
    we have a closing brace.
  • 0:42 - 0:47
    Now inside this TextView class,
    we have a bunch of variables here.
  • 0:47 - 0:50
    Each of these represents
    a field in a text view.
  • 0:50 - 0:53
    So we have a field for
    the text of the text view.
  • 0:53 - 0:56
    And a field for
    the text color of the text view.
  • 0:56 - 0:58
    There are other fields as well, but
  • 0:58 - 1:01
    remember this is a simplified
    version of the TextView class.
  • 1:01 - 1:04
    We also have a variable to store
    a reference to the context of
  • 1:04 - 1:06
    the application.
  • 1:06 - 1:09
    You can read more about context in
    the instructor notes below, but
  • 1:09 - 1:12
    its basically a way to access
    information about the app.
  • 1:13 - 1:17
    Like for example, you can access
    resources in the app using the context.
  • 1:18 - 1:20
    And these are the methods
    of the TextView.
  • 1:20 - 1:24
    The setText method takes
    this input a text string.
  • 1:24 - 1:29
    Then it updates the global variable
    mText to use this new value.
  • 1:29 - 1:32
    Similarly, the setTextColor
    takes this input a new color.
  • 1:32 - 1:36
    And it updates the internal state
    to have this new color value.
  • 1:36 - 1:39
    There's also methods like getText and
    getTextColor,
  • 1:39 - 1:43
    which return the internal text string or
    the internal TextColor.
  • 1:44 - 1:48
    One more thing, you may have noticed
    that these global variables here,
  • 1:48 - 1:50
    all start with a lowercase m.
  • 1:50 - 1:52
    M stands for member variable and
  • 1:52 - 1:55
    these are all member variables
    of this TextView class.
  • 1:55 - 1:59
    Member variable is just
    another way to say, field or
  • 1:59 - 2:01
    the state of the TextView class.
  • 2:01 - 2:03
    Here's a simplified image view class.
  • 2:03 - 2:05
    You would find it in
    the imageview.java file.
  • 2:06 - 2:11
    It starts with public class, and
    then ImageView is the name of the class.
  • 2:11 - 2:14
    And then we have opening and
    closing braces.
  • 2:14 - 2:16
    This makes up the state
    of the image view.
  • 2:16 - 2:18
    Normally it would have
    more fields than this, but
  • 2:18 - 2:20
    I just want to show you an example.
  • 2:20 - 2:24
    This is an integer variable and
    it's called mImageId.
  • 2:24 - 2:26
    M for member variable.
  • 2:26 - 2:28
    This stores the resource ID for
  • 2:28 - 2:31
    the image that should be
    shown in this image view.
  • 2:31 - 2:34
    Down here we have the constructor
    which is used to construct
  • 2:34 - 2:39
    a new image view object, and
    it takes it's input, a context, and
  • 2:39 - 2:41
    then it initializes these variables.
  • 2:42 - 2:46
    Then we have a method, it's called
    setImage and it takes its input,
  • 2:46 - 2:50
    a resource ID, for the new image that
    should be shown in this image view.
  • 2:50 - 2:54
    Inside this method, we update
    the internal variable for image ID.
  • 2:55 - 2:59
    Again, we created these simplified
    TextView and ImageView classes,
  • 2:59 - 3:02
    to help you understand
    the concept of classes.
  • 3:02 - 3:05
    Now let's look at the real
    version of these classes inside
  • 3:05 - 3:06
    the Android framework.
  • 3:08 - 3:09
    To see the code for
  • 3:09 - 3:13
    the Java classes in Android,
    you can install this Chrome extension.
  • 3:13 - 3:16
    If you're not using the Chrome browser
    you can see the instructor notes
  • 3:16 - 3:18
    below for how to access these files.
  • 3:19 - 3:20
    Okay.
  • 3:22 - 3:26
    Once that Chrome extension is installed,
    then when I search for something like
  • 3:26 - 3:31
    TextView Android, and I go look at the
    reference documentation for this class,
  • 3:31 - 3:35
    then there's going to be this extra
    link here that says view source.
  • 3:35 - 3:37
    This is really for advanced developers.
  • 3:37 - 3:41
    If you're curious about the code that
    is used to create this TextView.
  • 3:41 - 3:46
    This is a TextView Java file that was
    defined by someone on the Android team.
  • 3:46 - 3:47
    As you can see, it's really long and
  • 3:47 - 3:50
    complex, but
    it still has the main parts of a class.
  • 3:51 - 3:55
    It starts with public class, and then
    TextView, the name of the class, and
  • 3:55 - 3:57
    then this is the opening brace and
  • 3:57 - 4:00
    the closing brace is all
    the way at the bottom here.
  • 4:01 - 4:04
    For example,
    we see that TextColor is a field.
  • 4:05 - 4:06
    We also see that Text is a field.
  • 4:06 - 4:11
    And here's the constructor for TextView
    that takes it's input a Context.
  • 4:11 - 4:16
    If I hit Command+F or Ctrl+F if you're
    on Windows, then I can try to search for
  • 4:16 - 4:20
    settext, and here's the setText method.
  • 4:20 - 4:24
    This method is going to set
    the string value of the TextView, and
  • 4:24 - 4:25
    if I search for getText and
  • 4:25 - 4:29
    I also see this method returns the text
    that the TextView is displaying.
  • 4:29 - 4:32
    You get the idea, and you can browse
    through this on your own if you want.
  • 4:33 - 4:36
    If I search for
    the ImageView documentation page,
  • 4:36 - 4:40
    then I can click on view source to view
    all the code for the ImageView class.
  • 4:40 - 4:43
    The file says public
    class image view and
  • 4:43 - 4:46
    then these are the fields that make
    up the state of the ImageView,
  • 4:46 - 4:50
    and then there's a bunch of
    methods at the bottom of the file.
  • 4:50 - 4:52
    The code in this file is
    pretty complex as well and
  • 4:52 - 4:54
    it's also thousands of lines of code.
  • 4:54 - 4:57
    The best part about object-oriented
    programming though.
  • 4:57 - 5:00
    Is that you don't need to know
    about all of these details.
  • 5:00 - 5:04
    As a developer, you can use TextView and
    ImageView in your app.
  • 5:04 - 5:07
    Without knowing about all the gory
    details of what's going on inside of it.
Title:
What Are Java Objects? Pt. 2
Video Language:
English
Team:
Udacity
Project:
UD837 - Android for Beginners
Duration:
05:09

English subtitles

Revisions