Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed Java Objects!
      
    
You have completed Java Objects!
Preview
    
      
  Objects are created from blueprints, or classes. Let's make one.
Learn more
- You might not have seen a PEZ Dispenser before. I hope you can experience it someday if you haven't. It's a lot of fun.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
                      Objects and software allow us to express
and model things that we have and
                      0:00
                    
                    
                      use in real life.
                      0:04
                    
                    
                      Programmers have discovered
that all real world
                      0:06
                    
                    
                      objects share 2 important characteristics
they have state and behavior.
                      0:08
                    
                    
                      By creating an object in code
that maintains its own state and
                      0:13
                    
                    
                      presents its behavior for usage.
                      0:16
                    
                    
                      It allows you to hide how things
are actually working from
                      0:18
                    
                    
                      users of your object.
                      0:20
                    
                    
                      A great example of this is a radio
that has some state is it on or
                      0:22
                    
                    
                      off what station is it?
                      0:27
                    
                    
                      [MUSIC]
And
                      0:29
                    
                    
                      it also has some behavior that
it exposes to the use for
                      0:29
                    
                    
                      the power on change the station.
                      0:32
                    
                    
                      How does it actually work?
                      0:35
                    
                    
                      I have no idea.
                      0:36
                    
                    
                      Its inner workings are hidden from me but
its behavior is exposed and
                      0:38
                    
                    
                      I can manipulate the state
that allows me to change.
                      0:42
                    
                    
                      Before we go any deeper I
want to apologize to you for
                      0:45
                    
                    
                      what I'm about to do to your brain.
                      0:49
                    
                    
                      Chances are after I introduce
this concept to you,
                      0:51
                    
                    
                      you will not be able to stop
thinking about how you would create
                      0:54
                    
                    
                      everything you see in real
life as an object in code.
                      0:58
                    
                    
                      Like an ear worm when somebody sings,
We Built This City.
                      1:01
                    
                    
                      You're gonna be building
an object representation
                      1:05
                    
                    
                      of every single thing you see.
                      1:08
                    
                    
                      Sorry about that.
                      1:11
                    
                    
                      Okay, so
let's explore a real life object together.
                      1:12
                    
                    
                      How about this Yoda Pez
dispenser right here.
                      1:15
                    
                    
                      Now, if you haven't seen
a Pez dispenser before,
                      1:17
                    
                    
                      they're a little toy that serves
candy when you flip back their head.
                      1:20
                    
                    
                      They usually have different
character heads on them and
                      1:22
                    
                    
                      they're collector's items, like this one
here from Star Wars, the hero, Yoda.
                      1:24
                    
                    
                      So, let's see,
it definitely has some state.
                      1:28
                    
                    
                      Is it empty?
                      1:31
                    
                    
                      Nope.
                      1:32
                    
                    
                      How many are in there?
                      1:33
                    
                    
                      It looks like there's about ten.
                      1:34
                    
                    
                      There's also some behavior, right.
                      1:36
                    
                    
                      Namely its job: dispense when we do this.
                      1:38
                    
                    
                      It changes the state of the Pez dispenser.
                      1:40
                    
                    
                      Now there are ten and now there's nine and
it can also be loaded
                      1:42
                    
                    
                      like I could add more Pez in here and
would also change state.
                      1:47
                    
                    
                      Come to think of it.
                      1:51
                    
                    
                      All Pez dispensers kind of work the same.
                      1:52
                    
                    
                      They just have different character heads.
                      1:55
                    
                    
                      If we imagine the factory
where these are made.
                      1:57
                    
                    
                      I bet there is some sort of
master blueprint of the Pez
                      1:59
                    
                    
                      dispenser that is used to
create each one of these.
                      2:02
                    
                    
                      And then customizes the character for
each one that comes through.
                      2:05
                    
                    
                      This blueprint used to create
objects in Java is called a class.
                      2:08
                    
                    
                      Could we build something like
this blueprint in code and
                      2:13
                    
                    
                      then create objects from it?
                      2:15
                    
                    
                      We definitely can and
it's pretty straightforward.
                      2:16
                    
                    
                      Let's do just that.
                      2:19
                    
                    
                      Okay, so what we'll do is
we'll create a new class and
                      2:21
                    
                    
                      then use it in a console application.
                      2:24
                    
                    
                      Now, I've gone ahead and I've built
the console application boilerplate for
                      2:26
                    
                    
                      us again it's in a file called
Example.java, let's open that up.
                      2:30
                    
                    
                      So you might have used the java.io
console object before to take input and
                      2:35
                    
                    
                      write out to the screen.
                      2:39
                    
                    
                      I've had several students ask what
the difference between the console and
                      2:40
                    
                    
                      something that they've seen used
elsewhere and that's system.out.
                      2:44
                    
                    
                      Well, the answer is that console is
actually just a convenient wrapper around
                      2:47
                    
                    
                      system out and system in.
                      2:51
                    
                    
                      We aren't going to be taking any input
right now let's take a look at this common
                      2:52
                    
                    
                      pattern for writing out to the console.
                      2:56
                    
                    
                      So System is a class that's automatically
imported, right? So System, and
                      2:58
                    
                    
                      it provides a static public field
named out. Out is a print stream and
                      3:02
                    
                    
                      it exposes some methods that we've
seen before on the console object.
                      3:07
                    
                    
                      Now, don't worry [LAUGH] about
that mouthful that I just said
                      3:11
                    
                    
                      just know that we can write out
to the screen using .println.
                      3:14
                    
                    
                      So println means print line and what
that means is it will add a new line at
                      3:19
                    
                    
                      the end it's kind of a convenience method,
right, to the printf. Or in printf we
                      3:23
                    
                    
                      were doing the %n we don't need to
do that with println, so let's just do that.
                      3:27
                    
                    
                      So we're saying we are making
a new Pez dispenser.
                      3:30
                    
                    
                      Okay, so let's do it.
                      3:39
                    
                    
                      Let's make a new file
called PezDispenser.java.
                      3:40
                    
                    
                      So what we'll do is we'll right click
over here and we'll say new file, and
                      3:44
                    
                    
                      we'll make a Pez dispenser and
the case matters PezDispenser.java.
                      3:47
                    
                    
                      All right, so let's create this class.
                      3:53
                    
                    
                      So the class keyword allows us to
specify that this is in fact the class.
                      3:56
                    
                    
                      So class and then it's this
is similar to camel case but
                      4:01
                    
                    
                      the first letter is capitalized,
so we're gonna do PezDispenser.
                      4:04
                    
                    
                      And then we're going to open and close.
                      4:08
                    
                    
                      Now believe it or not.
                      4:12
                    
                    
                      That's actually enough to
allow us to create an object.
                      4:14
                    
                    
                      But let's add a little bit more info, so
that we can show that things are working.
                      4:17
                    
                    
                      Let's add some state.
                      4:20
                    
                    
                      How about the name of the character whose
head appears under the 'spence, all right.
                      4:21
                    
                    
                      So what we'll do is we'll add a field or
a member of variable.
                      4:25
                    
                    
                      So anything between these two brackets.
                      4:29
                    
                    
                      This block of code it's
known as a class scope.
                      4:33
                    
                    
                      So we'll do in here will just create a new
variable and this will look very familiar.
                      4:35
                    
                    
                      We'll make a new String.
                      4:38
                    
                    
                      And it's a character name and
we'll set that to Yoda.
                      4:41
                    
                    
                      You've seen blocks used to define scope
like when we're doing a while loop or
                      4:45
                    
                    
                      a conditional.
                      4:51
                    
                    
                      Opening a block with a curly brace and
then you close it.
                      4:53
                    
                    
                      It opens a new scope and
classes aren't any different but
                      4:57
                    
                    
                      we'll explore this more here in a bit.
                      4:59
                    
                    
                      So, first though let's go use our class so
remember to save this file,
                      5:02
                    
                    
                      and we'll go over here to example.
                      5:05
                    
                    
                      And let's make a brand new object using
our Pez dispenser class as the blueprint.
                      5:08
                    
                    
                      So first what we do is we declare the type
of variables of the type of variable like
                      5:14
                    
                    
                      normally we would do it.
                      5:19
                    
                    
                      String here we're going
to do a Pez dispenser.
                      5:20
                    
                    
                      And then we're gonna name
a variable like we always do.
                      5:24
                    
                    
                      Let's call it dispenser.
                      5:26
                    
                    
                      And here we're going to be
creating a brand new one.
                      5:28
                    
                    
                      So we're gonna use the keyword new.
                      5:33
                    
                    
                      So a new PezDispenser.
                      5:36
                    
                    
                      And we're going to open it and
close it just like that.
                      5:40
                    
                    
                      So the out object on system so System.out.
                      5:44
                    
                    
                      Also has the printf method
that we've been using.
                      5:48
                    
                    
                      So we say printf and then we're
going to put in our format strings.
                      5:51
                    
                    
                      So we're gonna say the dispenser is and
                      5:56
                    
                    
                      we're gonna put in
a placeholder to replace there.
                      6:00
                    
                    
                      So let's say %s and then remember printf
doesn't automatically put the new ones in
                      6:03
                    
                    
                      for us, so we need to do 
%n the new line, okay.
                      6:07
                    
                    
                      And then, we can access the actual
field that we put over there
                      6:11
                    
                    
                      by saying dispenser.
                      6:16
                    
                    
                      And we're going to use the dot notation
on this new object that we created
                      6:18
                    
                    
                      to say .characterName.
                      6:21
                    
                    
                      And we'll close that system printer,
all right.
                      6:24
                    
                    
                      So let's go over this really quick.
                      6:27
                    
                    
                      So this line here.
                      6:28
                    
                    
                      We instantiate a new PezDispenser
object and it creates a new
                      6:30
                    
                    
                      instance this object here is referred
to as an instance of type PezDispenser.
                      6:36
                    
                    
                      Now because PezDispenser.java is in
the same folder as our Example.java and
                      6:42
                    
                    
                      we haven't done anything yet to package
up our code, we'll do that later.
                      6:49
                    
                    
                      We don't need to actually import
PezDispenser, it's just there and
                      6:52
                    
                    
                      that's because they're in the same folder.
                      6:56
                    
                    
                      So our example program can simply just
access the class by using its name.
                      6:59
                    
                    
                      We'll talk more about packaging and
importing later on.
                      7:03
                    
                    
                      So but for now let's just run this.
                      7:06
                    
                    
                      So let's come down here and
we'll say clear and javac.
                      7:09
                    
                    
                      We're just gonna compile
Example.java and then
                      7:13
                    
                    
                      we're gonna run example
the program Example Cool.
                      7:18
                    
                    
                      So it says here's a print line adding
the PezDispenser to add the new line and
                      7:24
                    
                    
                      that's that the PezDispenser is Yoda and
it's pulling off that character name.
                      7:27
                    
                    
                      Did you notice how the PezDispenser
got automatically compiled, so
                      7:31
                    
                    
                      if we refresh over here, you'll
see that there is a PezDispenser
                      7:34
                    
                    
                      class I got automatically compiled without
us needing to call the javac on it.
                      7:39
                    
                    
                      Now that's because in order for
                      7:43
                    
                    
                      Example.java to use it it needed to
have access to the PezDispenser code.
                      7:44
                    
                    
                      So it compiled it.
                      7:48
                    
                    
                      So this is looking pretty good, right?
                      7:49
                    
                    
                      So we created a new
object named dispenser and
                      7:51
                    
                    
                      accessed its characterName
field which was exposed.
                      7:54
                    
                    
                      What I wonder if we could actually
change that character name.
                      7:58
                    
                    
                      That would be bad wouldn't it?
                      8:04
                    
                    
                      You can't really change the PezDispenser's
character after it's been created.
                      8:06
                    
                    
                      So let's go ahead and
let's try to change that.
                      8:10
                    
                    
                      Let's change it to Darth Vader and
                      8:14
                    
                    
                      see if we can't cause
a disturbance in reality.
                      8:20
                    
                    
                      So we'll say Darth Vader.
                      8:24
                    
                    
                      And then let's go ahead we'll
save this and then run it.
                      8:28
                    
                    
                      No!
                      8:34
                    
                    
                      We better fix that soon.
                      8:37
                    
                    
                      >> Awesome.
                      8:39
                    
                    
                      Now we have a blueprint, or class, that we
can use to finish modeling our example.
                      8:40
                    
                    
                      There are a few bad practices in
what we're doing right now, but
                      8:45
                    
                    
                      it is a great starting block.
                      8:48
                    
                    
                      We learned about fields and how we can
access them on newly created objects, or
                      8:50
                    
                    
                      as they're often referred to, instances.
                      8:54
                    
                    
                      So, in order to hide the inner
workings of the class.
                      8:57
                    
                    
                      So we can protect the Pez Dispenser
head swapping problem.
                      9:00
                    
                    
                      We're going to need to pick up a few
new tricks which we'll get to in
                      9:03
                    
                    
                      the next video.
                      9:06
                    
                    
                      Now before you ask,
                      9:08
                    
                    
                      when would we ever need to create
a Pez Dispenser in code in real life?
                      9:10
                    
                    
                      Let me answer you with
this you'd be surprised.
                      9:14
                    
                    
                      As a developer your skills are needed
by all sorts of industries and markets.
                      9:18
                    
                    
                      Everyone wants a website.
                      9:22
                    
                    
                      Everyone wants an app.
                      9:24
                    
                    
                      When researching Pez for this course,
                      9:26
                    
                    
                      I found that there is actually a new
site that is allowing you to 3 D printer
                      9:28
                    
                    
                      your head onto a Pez Dispenser. Guess
what they have on their website?
                      9:31
                    
                    
                      a way for you to upload your head and
                      9:36
                    
                    
                      simulate a Pez Dispenser to
see what it might look like.
                      9:37
                    
                    
                      Someone had to write that.
                      9:41
                    
                    
                      Now one time when I was
doing some consulting work,
                      9:44
                    
                    
                      I had to write code to simulate
a go cart going around a track.
                      9:46
                    
                    
                      It gives me an idea.
                      9:52
                    
                    
                      Let's do a quick exercise to check
our syntax on creating classes and
                      9:53
                    
                    
                      then go fix those books.
                      9:57
                    
              
        You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up