Return to Video

Create an Object

  • 0:01 - 0:03
    In the last video, I briefly
    mentioned that a constructor is
  • 0:03 - 0:08
    defined within the class, and it's used
    to actually create the object instances.
  • 0:08 - 0:11
    So, we call the TextView constructor
    to create this instance,
  • 0:11 - 0:14
    and we call it again to create
    another instance, and so on.
  • 0:14 - 0:17
    Let's look at the code for
    how to actually call a constructor.
  • 0:18 - 0:22
    Do you remember how we created a new
    integer variable named quantity and
  • 0:22 - 0:25
    we set it to have
    an initial value of two?
  • 0:25 - 0:26
    Creating a new object and
  • 0:26 - 0:29
    storing it inside a variable is
    going to follow a similar format.
  • 0:30 - 0:32
    It'll look something like this.
  • 0:32 - 0:36
    Instead of the int data type though,
    we have an object data type.
  • 0:36 - 0:39
    The object data type is whatever
    we call the class name.
  • 0:39 - 0:42
    In this case, it's TextView.
  • 0:42 - 0:46
    It can be anything as long as it follows
    the conventions for variable names.
  • 0:46 - 0:48
    So, we just call it priceTextView.
  • 0:48 - 0:50
    Then we set it equal to new object.
  • 0:50 - 0:52
    But we can break down this
    right hand side further.
  • 0:53 - 0:58
    The right hand side follows the format
    of new followed by the object data type,
  • 0:58 - 1:00
    which is TextView in this case.
  • 1:00 - 1:04
    This is calling the constructor
    defined in a TextView class so
  • 1:04 - 1:08
    we have parentheses with the input
    arguments to the constructor.
  • 1:08 - 1:11
    So, if the constructor
    requires a context object,
  • 1:11 - 1:13
    then we need to pass a context here.
  • 1:13 - 1:18
    Remember that the context object helps
    you access application resources and
  • 1:18 - 1:20
    other things about
    the application environment.
  • 1:21 - 1:24
    So, altogether this is
    creating a new TextView object
  • 1:24 - 1:26
    by calling the constructor and
  • 1:26 - 1:31
    then it's storing this new object
    inside a variable called priceTextView.
  • 1:31 - 1:35
    And this variable can only
    hold a single TextView object
  • 1:35 - 1:36
    as defined by its data type.
  • 1:37 - 1:40
    Here's another example of
    creating an ImageView object and
  • 1:40 - 1:42
    storing it inside a variable.
  • 1:42 - 1:46
    The data type is ImageView,
    the variable name is coffeeImageView.
  • 1:46 - 1:50
    And then we have the assignment
    operator followed by new ImageView.
  • 1:50 - 1:54
    This is spelled the same exact
    way as the object data type here.
  • 1:54 - 1:57
    Since we're calling the constructor and
    we have to pass in any inputs,
  • 1:57 - 2:01
    we have parentheses and
    then we put the inputs in there.
  • 2:01 - 2:03
    Here's a third example that
    creates a button object and
  • 2:03 - 2:06
    stores it in this variable.
  • 2:06 - 2:08
    The date type is Button,
    the variable name is submitOrderButton,
  • 2:08 - 2:12
    and then we say,
    new Button to call the constructor.
  • 2:13 - 2:16
    You can even define your
    own object data types.
  • 2:16 - 2:20
    Say, for example, I created
    a class definition for CatView.
  • 2:20 - 2:23
    Then I could use this line
    to create a new CatView.
  • 2:23 - 2:25
    In this case, the constructor
    requires an input string for
  • 2:25 - 2:29
    the name of the cat so that's why
    I put Tiger in parentheses and
  • 2:29 - 2:32
    pass that in to the CatView constructor.
  • 2:32 - 2:36
    To determine what to pass as
    input to these constructors you
  • 2:36 - 2:41
    have to look at either the class
    Javadoc file or the class source file.
  • 2:41 - 2:46
    All these cases use the word
    new to create a new object.
  • 2:46 - 2:48
    If we go back to the Java keyword list,
  • 2:48 - 2:51
    then indeed we do find that
    the word new is in that list.
  • 2:52 - 2:55
    Sometimes when you're
    looking at the documentation,
  • 2:55 - 2:59
    it may advise you to use a factory
    method to create an object.
  • 2:59 - 3:02
    That is instead of a constructor.
  • 3:02 - 3:03
    Here's an example.
  • 3:03 - 3:04
    To create a new MediaPlayer,
  • 3:04 - 3:09
    the documentation advises you to
    use the method MediaPlayer.create.
  • 3:09 - 3:13
    And this returns a MediaPlayer object
    that can be stored inside this variable.
  • 3:13 - 3:17
    It follows the format of object
    data type, the MediaPlayer with
  • 3:17 - 3:23
    a capital m dot and the factory method
    name, which is create in this case.
  • 3:23 - 3:26
    Then you pass in the input arguments
    to initialize that media player.
  • 3:27 - 3:30
    Here's another example of
    how to create a Toast.
  • 3:31 - 3:34
    A toast is a message that pops
    up temporarily in your app and
  • 3:34 - 3:34
    then disappears.
  • 3:35 - 3:39
    So the code starts with Toast
    with a capital T because
  • 3:39 - 3:40
    this is the object data type.
  • 3:40 - 3:44
    And then we have a dot and then makeText
    which is the factory method name.
  • 3:44 - 3:49
    And then we pass the input
    arguments to create this Toast.
  • 3:49 - 3:52
    To know what the factory method name
    is and what the input arguments are,
  • 3:52 - 3:55
    you have to check the documentation for
    the Toast class.
  • 3:55 - 3:58
    In general, in order to know whether
    you should create an object using
  • 3:58 - 4:02
    a constructor or with a factory method,
    you can just Google search for
  • 4:02 - 4:04
    how to create that object.
  • 4:04 - 4:07
    Using a constructor is the most
    common way to create a new object.
  • 4:07 - 4:10
    It's less common that you would need to
    use a factory method, but to know for
  • 4:10 - 4:13
    sure which one you should use,
    you can just Google search for
  • 4:13 - 4:14
    how to create that object.
  • 4:15 - 4:18
    Here's a chance to practice
    with creating objects.
  • 4:18 - 4:20
    In each of these lines of code,
    there's an error.
  • 4:21 - 4:22
    Figure out what the error is and
  • 4:22 - 4:25
    then write the correct line
    of code in these boxes below.
タイトル:
Create an Object
概説:

12-27 Create an Object

more » « less
Video Language:
English
Team:
Udacity
プロジェクト:
UD837 - Android for Beginners
Duration:
04:27

English subtitles

改訂 Compare revisions