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
    
      
  Constants are values that never change. Let's use the `static` keyword to provide information at the class level instead of the instance level.
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
                      [MUSIC]
                      0:00
                    
                    
                      All right, let's work on getting some
more state represented in our object.
                      0:04
                    
                    
                      I want to get some Pez in here so
we can do its job dispense.
                      0:09
                    
                    
                      But first, in case you aren't
that big of a fan of Pez.
                      0:13
                    
                    
                      You might not know some vital information
that we're going to need for our coding.
                      0:16
                    
                    
                      So Pez refills come in
a packs of 12 like this.
                      0:21
                    
                    
                      And that happens to be exactly the same
count as what the standard Pez dispenser
                      0:25
                    
                    
                      can contain and no more.
                      0:29
                    
                    
                      12 is the maximum limit that our object
can contain, and that limit won't change,
                      0:31
                    
                    
                      it will remain constant.
                      0:36
                    
                    
                      We should express this limitation
in code before people start
                      0:38
                    
                    
                      filling it up, sound good?
                      0:41
                    
                    
                      Let's do this.
                      0:43
                    
                    
                      So the vital information that we
just talked about of how many Pez
                      0:45
                    
                    
                      can be stored in dispenser.
                      0:48
                    
                    
                      It's going to prove crucial
to how our object works.
                      0:49
                    
                    
                      Now, this number of Pez that can fit in
a dispenser is something that will never
                      0:52
                    
                    
                      change, right?
                      0:56
                    
                    
                      Values that do not change are often
referred to as constants.
                      0:57
                    
                    
                      A naming convention that helps explain
that a variable is a constant is to make
                      1:01
                    
                    
                      all letters uppercase and
separate the words with an underscore.
                      1:04
                    
                    
                      So let's add a max pez
constant to our class.
                      1:08
                    
                    
                      So we'll just come up here.
                      1:12
                    
                    
                      We'll say, public int MAX_PEZ,
                      1:14
                    
                    
                      and like we said, that was 12.
                      1:18
                    
                    
                      Okay, so let's imagine that this object
gets created as a currently stands in our
                      1:23
                    
                    
                      implementation MAX_PEZ is public.
                      1:26
                    
                    
                      So as you know,
                      1:30
                    
                    
                      anyone could change the limit of how
many Pez this can be loaded with.
                      1:31
                    
                    
                      That's not actually how things
work in real life, right?
                      1:35
                    
                    
                      I can't do that with
an actual Pez dispenser.
                      1:38
                    
                    
                      So again, we should make sure
that our objects reflect reality.
                      1:40
                    
                    
                      We can make it reflect reality with,
you guessed it, our old friend final.
                      1:44
                    
                    
                      So let's go ahead and save this.
                      1:49
                    
                    
                      So let's poke at it in
jshell really quick.
                      1:51
                    
                    
                      So I'm gonna open up jshell down here.
                      1:53
                    
                    
                      And jshell allows you to open classes,
which is pretty cool.
                      1:55
                    
                    
                      So to open your code, you just do a / and
then the command open.
                      1:59
                    
                    
                      And then you can just start typing
your class and open PezDispenser.java.
                      2:03
                    
                    
                      We're gonna open up as PezDipenser.java.
                      2:08
                    
                    
                      So now, what that does is PezDispenser
actually lives in the code here.
                      2:10
                    
                    
                      So we can say, PezDispenser.
                      2:14
                    
                    
                      And we'll just call it pd,
that's bad, right?
                      2:18
                    
                    
                      That's bad, what does PD mean?
                      2:21
                    
                    
                      Letter, Police Department?
                      2:21
                    
                    
                      I don't know.
                      2:22
                    
                    
                      All right, so I'm gonna say, Yoda.
                      2:24
                    
                    
                      Okay, and if we wanted to, now,
                      2:26
                    
                    
                      we can see that the MAX_PEZ exists.
                      2:30
                    
                    
                      MAX_PEZ, okay.
And if we try to change that
                      2:34
                    
                    
                      constant 123 in there.
                      2:38
                    
                    
                      You can assign a value to find a variable,
MAX_PEZ, cool.
                      2:42
                    
                    
                      So we've blocked the value from changing.
                      2:45
                    
                    
                      And our object is now this much
closer to representing reality.
                      2:46
                    
                    
                      Now, this concept of providing
vital constant information
                      2:51
                    
                    
                      about your class is super common.
                      2:53
                    
                    
                      Here, let's take a look at
the integer class actually.
                      2:55
                    
                    
                      So if we got an integer, and I come here.
                      2:57
                    
                    
                      Now, jshell like we said,
offers tab completion.
                      3:00
                    
                    
                      So let's leave Integer and
then we'll do a tab.
                      3:02
                    
                    
                      And look at all the uppercase
values there, so here we got BYTES.
                      3:05
                    
                    
                      We got MAX_VALUE, MIN_VALUE, so let's
see what the max value of integer is.
                      3:10
                    
                    
                      So that's how big a number
can be stored in.
                      3:17
                    
                    
                      But you know what?
                      3:19
                    
                    
                      That's right off the class.
                      3:20
                    
                    
                      And that's because it's true of every
integer that will ever be created.
                      3:22
                    
                    
                      Now, can we do that in our class?
                      3:26
                    
                    
                      I see that we did it on our instance,
right?
                      3:28
                    
                    
                      But can we do it like this?
                      3:30
                    
                    
                      Can we say, PezDispenser.MAX_PEZ.
                      3:31
                    
                    
                      And it says,
                      3:37
                    
                    
                      a non static variable MAX_PEZ cannot
be a referenced from a static context.
                      3:38
                    
                    
                      Nope, it doesn't work.
                      3:43
                    
                    
                      But this constant is true of
all PezDispensers that will
                      3:45
                    
                    
                      ever be created from this class.
                      3:47
                    
                    
                      So it should be defined on the class.
                      3:50
                    
                    
                      We should need to create an instance.
                      3:51
                    
                    
                      We should be able to act
just like Integer did.
                      3:53
                    
                    
                      So as you might have guessed by now,
it's just another keyword.
                      3:55
                    
                    
                      Now, the problem here is that the keyword
isn't very clear as to what it's doing.
                      3:58
                    
                    
                      The keyword is static.
                      4:02
                    
                    
                      So let's put it up here and
get it in here.
                      4:04
                    
                    
                      So say static,
I'm gonna save the file again.
                      4:06
                    
                    
                      And then let's reopen our Pez dispenser.
                      4:10
                    
                    
                      So if you say, open ns2p in our tab,
PezDispenser.java.
                      4:13
                    
                    
                      Cool, and I'm gonna control
L to clear the screen here.
                      4:19
                    
                    
                      And if I do PezDispenser.MAX_PEZ.
                      4:21
                    
                    
                      Now, these are sometimes also referred to
as class level variables because you can
                      4:29
                    
                    
                      access them at the class level,
as opposed to the instance.
                      4:33
                    
                    
                      And the error from before,
it makes a little bit more sense, right?
                      4:36
                    
                    
                      It said,
                      4:39
                    
                    
                      non-static variable MAX_PEZ cannot
be referenced from a static context.
                      4:40
                    
                    
                      Now, this class level is called the static
context versus the instances context.
                      4:45
                    
                    
                      Now, the keyword,
                      4:50
                    
                    
                      static actually refers to how
the variable itself is stored in memory.
                      4:51
                    
                    
                      But what it actually allows you to do
is the ability to provide variables and
                      4:55
                    
                    
                      methods directly off your class as
opposed to having them on the instance.
                      5:00
                    
                    
                      So actually here,
                      5:06
                    
                    
                      the auto complete in jshell will show
you what's available statically, right?
                      5:07
                    
                    
                      If we do Integer again and we press tab.
                      5:12
                    
                    
                      There's one we've seen before, parseInt.
                      5:13
                    
                    
                      So that's a great example of a static
method that you've already used, right?
                      5:17
                    
                    
                      So remember that it'll let you take
a string parseInt and return the number.
                      5:20
                    
                    
                      See, how it's right off the class?
                      5:29
                    
                    
                      Now, I don't think we need
any of those in our class.
                      5:30
                    
                    
                      But I thought I'd show you
since you've seen that already.
                      5:32
                    
                    
                      And when I showed you before, I had said,
                      5:35
                    
                    
                      there's a static method off of
the Integer class named parseInt.
                      5:37
                    
                    
                      And before that,
probably just sounded like mumbo jumbo.
                      5:40
                    
                    
                      But now,
it should be starting to be more familiar.
                      5:43
                    
                    
                      So now, if we pop back over to
Example.java before anything,
                      5:46
                    
                    
                      we can use it because it's now public and
static, so let's do that.
                      5:51
                    
                    
                      So we're making a PEZ Dispenser.
                      5:54
                    
                    
                      So let's add a new System.out,
                      5:56
                    
                    
                      we'll do a print line, or
let's do printf actually.
                      5:59
                    
                    
                      We'll say, ("FUN FACT: There are %d
                      6:04
                    
                    
                      PEZ allowed in every dispenser",.
                      6:09
                    
                    
                      And we'll go ahead, and
                      6:14
                    
                    
                      we will push in our value before we
                      6:18
                    
                    
                      create PezDispenser.MAX_PEZ.
                      6:23
                    
                    
                      I should put a new line
at the end of this.
                      6:29
                    
                    
                      And see, how we didn't need
to have the instance at all?
                      6:35
                    
                    
                      This is before we initialized it.
                      6:38
                    
                    
                      A fun fact about Java specifically is that
because a variable is modified with static
                      6:40
                    
                    
                      and final, when the compilation happens,
when javac happens.
                      6:45
                    
                    
                      It replaces all instances of that
variable with the actual value.
                      6:49
                    
                    
                      Okay, so now that we know how to
not overfill these Pez Dispensers,
                      6:53
                    
                    
                      let's make sure we don't overfill
our brains with new concepts.
                      6:57
                    
                    
                      Why don't you take a quick break and
let this information sink in.
                      7:00
                    
                    
                      Now, during your break,
                      7:04
                    
                    
                      try to think of any other constants for
objects in the real world.
                      7:05
                    
                    
                      Are those constants unique
to that specific object or
                      7:10
                    
                    
                      all objects of that type?
                      7:12
                    
                    
                      Okay, I'll see in a bit.
                      7:15
                    
                    
                      Enjoy your break.
                      7:16
                    
                    
                      We built this city,
we built this city on rock and rule.
                      7:18
                    
              
        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