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.
Resources
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 in software allow us to express
0:01
and model things
we have and use in real life.
0:03
Programmers have discovered
that all real-world objects share
0:06
two important characteristics.
0:09
They have state and behavior.
0:11
By creating an object
in code that maintains its own state
0:14
and presents its behavior for usage,
you can hide
0:17
how things are actually working
from your users of your object.
0:20
A great example of this is a radio
that has some state.
0:23
Is it on or is it off?
0:27
And what station is it tuned to?
0:29
And it also has behavior
that it exposes to the user,
0:31
like turning the power on
or changing the station.
0:34
How does it work internally?
0:37
I have no idea.
0:39
Its inner workings are hidden from me,
but its behavior
0:40
is exposed, and I can manipulate the state
that it allows me to change.
0:43
Before we go deeper,
0:48
I want to apologize
for what I'm about to do to your brain.
0:49
Chances are, after I introduce
this concept, you might find yourself
0:52
constantly thinking about how
0:56
to create objects and code
for everything you see in real life.
0:57
You'll be mentally building object
representations of everything around you.
1:01
Sorry about that.
1:06
Okay, so let's explore
a real-life object together.
1:07
How about... Oh.
1:10
This little Pez dispenser.
1:15
Now, if you haven't seen one before,
1:16
it's a toy that serves candy
when you flip back its head.
1:18
They usually have different
1:21
character heads, like Donatello here,
or Yoda from Star Wars. See?
1:21
I also got this cute little kitty cat.
1:26
So, a Pez dispenser
definitely has some state, right?
1:29
Is it empty? Yep.
1:32
It also has behavior.
1:35
Mainly, it's job to dispense.
1:36
And when it does, it changes the state,
reducing the number of candies by one.
1:38
It can also be loaded with new candy,
which would also alter the state.
1:43
All Pez Dispensers,
no matter the character head, work
1:46
similarly
and are created from the same blueprint.
1:49
In Java, this blueprint used to create
objects is called a class.
1:52
We can build a class
that describes a Pez Dispenser in code,
1:57
and then create objects from it.
2:00
Let's do just that.
2:02
Okay, so what we'll do
2:05
is create a new class
and then use it in a console application.
2:06
Now I've gone ahead and created the starting
point for our console application
2:10
and it's in this file here, example.java.
Let's open that up.
2:13
So in Java Basics we used the java dot IO
2:19
console object
to take input and write out to the screen.
2:22
This was done so that things weren't
so overwhelming at first.
2:26
Now you may have seen things online
regarding something else
2:29
called system.out, and maybe you're
wondering what the difference is.
2:31
Either way, the answer is
that console is actually just a convenient
2:36
wrapper around system.out and system.in.
2:39
We aren't going to be taking any input
right now, so let's take a look at this
2:43
common pattern
2:46
for writing out to the console,
because I want you to be familiar with it.
2:47
So, System is a class
that's automatically imported.
2:51
So, System. This system
2:54
provides a static public field named Out.
2:57
Out is a print stream, and it exposes
some methods that we've seen before
3:00
on the console object.
3:04
Now, don't worry about that mouthful
I just said.
3:05
Just know that we can write out
to the screen using print line.
3:08
So println means print line,
3:11
and what that means is
it will actually add a new line at the end.
3:14
So how before we were using printf
3:18
and using percent n to signify a new line,
this will put a new line at the end
3:20
automatically. It's nice
if we don't need to format this output
3:25
with any other variables.
So we're saying we are making a new Pez
3:28
dispenser.
3:33
Okay, so let's do it
3:37
Let's make a new file
called PezDispenser.java.
3:38
So what we'll do is right-click over here
and select New File.
3:42
The case does matter
here, capital P and capital D.
3:46
In this
3:52
file,
we'll define a class named PezDispenser.
3:52
The class keyword indicates
that this is a class.
3:55
Class names
3:58
use something similar to camel case,
but it also starts with a capital letter.
3:59
This is called Pascal case.
4:03
Every word starts with a capital letter
and there are no spaces.
4:05
Cool, then we'll open and close
with our curly braces.
4:09
Now, believe it or not, that's actually
enough to allow us to create an object.
4:12
Let's add some details
so we can see it working.
4:16
Let's add some state.
4:19
How about the name of the character
whose head appears on the dispenser?
4:20
So what we'll do is we'll add a field,
or member variable,
4:24
to store the character's name.
4:28
So anything within these two curly
braces is what's known as the class scope.
4:30
Inside of this class scope,
we'll define a variable of type
4:35
string called character
name and set it to be Yoda.
4:38
You've seen blocks
4:45
used to define scope before,
like when we're doing a while loop
4:46
or a conditional, opening a block
with a curly brace and then you close it.
4:49
It opens a new scope
and classes aren't any different.
4:53
We'll explore this more in a bit.
4:57
So, first
though, let's go use our new class,
4:58
so remember to save this file,
and we'll go over here to example.
5:01
Now in our main program,
let's make a brand new object,
5:06
using our PezDispenser class
as the blueprint.
5:09
So just as we declare new variables
with the type first,
5:13
like string or int, here
we want to use our type, PezDispenser.
5:15
Then we name it like normal,
I'll name it dispenser.
5:21
Then for the assignment, we're creating
5:25
a brand new object,
so we use the new keyword.
5:27
Then we tell it it's a new PezDispenser
and apply our parentheses.
5:30
So system.out also has the printf method
we used before.
5:35
So let's use that here
to see what our object looks like.
5:39
I'll type system.out.printf and pass it
the format string,
5:42
the dispenser is,
and we'll put a placeholder here, %s.
5:49
And remember, printf
doesn't automatically do new lines for us,
5:53
so let's add a %n.
5:57
Then, as the second argument,
we can use our new dispenser variable,
5:59
and using dot notation,
we can access the character name field.
6:03
Let's break things down.
6:09
On this line,
we instantiate a new PezDispenser object,
6:10
and it creates a new instance of type
PezDispenser.
6:14
Because our PezDispenser.java file
is in the same folder as example.java,
6:18
there's no need to actually import
our PezDispenser.
6:23
It's just there.
6:25
We'll talk more about packaging
and importing later on.
6:27
For now, let's try and run this.
6:30
In the console, let's say clear
6:33
and javac example.java.
6:36
And then we're going to run example.
6:41
Cool so here our print line
you can see it broke
6:50
the next one to a new line
and here we see our character name
6:53
It creates the object and outputs
that the PezDispenser character is Yoda.
6:56
If we refresh our workspace here,
you'll notice that
7:01
the PezDispenser class is compiled
automatically.
7:03
This happens
because it's being used in example.java.
7:07
Since this file is dependent
on PezDispenser,
7:10
it will automatically compile
that as well.
7:13
Now we have a working class
and we created an instance of it,
7:15
then accessed its character name field
which was exposed.
7:18
Hmm, you know what?
7:21
I wonder what would happen
if we tried to change the character name.
7:24
That would be bad, right?
7:27
You can't change the character
after it's already been created, right?
7:28
Let's see what happens.
7:31
Let's try to
7:33
change our character
name value to Darth Vader
7:34
and see if we can cause a disturbance
in the force.
7:37
Let's run our code again.
7:44
Yikes!
7:51
You'll see that we're able
to alter our object and convert it
7:52
to the dark side.
7:55
We'll explore a fix for this in future
lessons.
7:56
Awesome!
8:00
Now we have a blueprint, or class, that
we can use to finish modeling our example.
8:01
There are a few bad practices
in what we're doing right now,
8:06
but it's a great starting block.
8:09
We learned about fields
and how we can access them
8:10
on newly created objects, or
as they're often referred to, instances.
8:13
So in order to hide
the inner workings of the class
8:18
so we can protect the Pez dispenser
head swapping problem, we're going to need
8:20
to pick up a few new tricks,
which we'll get into in the next video.
8:24
Now I know what you're probably thinking.
8:29
When would we ever need to create a Pez
dispenser in code in real life?
8:30
Well, let me answer you with this.
8:35
You'd be surprised.
8:37
As a developer, your skills are needed
by all sorts of industries and markets.
8:39
Everyone wants a website.
8:43
Everyone wants an app.
8:45
When researching PEZ for this course,
I found that there is actually a site
8:47
that is allowing you to 3D
print your head onto a PEZ dispenser.
8:51
Guess what they have on their website?
8:56
A way for you to upload your head
and simulate a PEZ
8:58
dispenser to see what it might look like.
9:00
So someone had to write that.
9:03
So let's do a quick exercise
to check our syntax on creating classes
9:05
and then go squash those bugs.
9:09
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