1
00:00:00,540 --> 00:00:03,090
A constant is a variable that can never change.

2
00:00:05,730 --> 00:00:08,430
A value that isn't supposed to change shouldn't.

3
00:00:10,880 --> 00:00:13,570
Constants are variables that can never change.

4
00:00:16,110 --> 00:00:19,320
In this lesson, you will define a static final constant.

5
00:00:22,180 --> 00:00:26,220
What is a constant, as I just said twice, a value that can never change?

6
00:00:29,900 --> 00:00:34,220
Why do we use constants to ensure values that shouldn't change, don't?

7
00:00:38,010 --> 00:00:43,680
And why are constant static, because they belong to the class, their value doesn't change regardless

8
00:00:43,680 --> 00:00:44,510
of the object.

9
00:00:50,110 --> 00:00:53,860
A field is not static because it has different values for each object.

10
00:00:57,210 --> 00:01:00,750
In other words, each field belongs to a particular object.

11
00:01:04,860 --> 00:01:10,800
A constant is the same across every object, regardless of the object, every employee works for the

12
00:01:10,800 --> 00:01:14,730
same company, so a constant belongs to the class.

13
00:01:19,900 --> 00:01:25,960
So why are constants final, final means you can never update the variable, a constant is a value that

14
00:01:25,960 --> 00:01:27,120
should never change.

15
00:01:27,130 --> 00:01:30,100
So the state of the constant needs to be final.

16
00:01:35,400 --> 00:01:40,740
Inside employed out Java, you'll notice that I'm plainly writing the company name Java Stars, this

17
00:01:40,740 --> 00:01:43,170
is an important value because it affects our code.

18
00:01:44,040 --> 00:01:47,340
Imagine I accidentally change it here, but here I leave it.

19
00:01:49,190 --> 00:01:55,210
This is bad because now everything is inconsistent and I'm vulnerable to getting bugs never play the

20
00:01:55,220 --> 00:01:58,430
right an important value, you might change it by mistake.

21
00:01:58,820 --> 00:02:00,680
Instead, use a constant.

22
00:02:06,170 --> 00:02:09,229
So here, instead of rewriting the same value over and over.

23
00:02:12,360 --> 00:02:14,610
I'm going to create a static final string.

24
00:02:18,500 --> 00:02:21,590
Constant company name Java stars.

25
00:02:27,370 --> 00:02:33,610
This is the naming convention for constants, every letter is uppercase and words are separated by an

26
00:02:33,610 --> 00:02:34,360
underscore.

27
00:02:52,580 --> 00:02:54,460
This information is in your cheat sheet.

28
00:02:56,150 --> 00:03:01,970
But what's important is that now, instead of writing the same value over and over, I can keep referencing

29
00:03:01,970 --> 00:03:02,810
the constant.

30
00:03:06,010 --> 00:03:09,070
And if you ask me, this is a lot cleaner and it's a lot safer.

31
00:03:10,910 --> 00:03:16,040
Should the constant be private, if there's no reason to make it public, then yes.

32
00:03:20,970 --> 00:03:26,280
The constant is final, so I'm not worried about the color accidentally changing it, the color could

33
00:03:26,280 --> 00:03:27,840
try, but it's not possible.

34
00:03:28,530 --> 00:03:29,370
Here, I'll show you.

35
00:03:36,270 --> 00:03:37,830
Java just won't let them see.

36
00:03:42,630 --> 00:03:47,490
So we could leave it as public, but there isn't a reason to make it publicly available outside the

37
00:03:47,490 --> 00:03:49,260
class, so it's good practice.

38
00:03:54,730 --> 00:04:00,000
And honestly, even if you want to expose the value for whatever reason, it's good practice to just

39
00:04:00,010 --> 00:04:02,620
give it a go getter that returns a copy of that value.

40
00:04:05,300 --> 00:04:10,790
You might be wondering why do we need a GETER that returns a copy of the constant value of the constant

41
00:04:10,790 --> 00:04:11,360
is final.

42
00:04:11,570 --> 00:04:14,510
There's no point, it's just good practice.

43
00:04:15,650 --> 00:04:20,360
If you decide not to include a getter and just make the constant public, that's fine, it's honestly

44
00:04:20,360 --> 00:04:21,589
just a matter of opinion.

45
00:04:27,170 --> 00:04:29,450
OK, now we're going to open up the movie star solution.

46
00:04:54,800 --> 00:04:57,980
Looking at the movie class, can you guess what needs to be a constant.

47
00:04:59,370 --> 00:05:06,750
The String Values DVD and Blu ray, what we did here by rewriting DVD and Blu ray over and over is extremely

48
00:05:06,750 --> 00:05:07,780
bad practice.

49
00:05:08,250 --> 00:05:09,940
We were basically playing with fire.

50
00:05:10,290 --> 00:05:14,010
Can you imagine if I misspelled one of them, I would get so many bugs.

51
00:05:16,720 --> 00:05:21,250
So what I'm going to do is make a constant for DVD private static Final String.

52
00:05:25,390 --> 00:05:26,620
Format DVD.

53
00:05:29,460 --> 00:05:30,660
Is equal to DVD.

54
00:05:33,000 --> 00:05:34,410
And a constant for Blu ray.

55
00:05:37,400 --> 00:05:39,220
Format Blu ray is equal to Blu ray.

56
00:05:55,230 --> 00:05:57,990
Now, I can refer to each constant where I need to.

57
00:06:42,190 --> 00:06:47,440
All right, let's recap a constant, the static, because it belongs to the class, it does not belong

58
00:06:47,440 --> 00:06:51,370
to an object, a constant is final because it should never change.

59
00:06:55,840 --> 00:06:59,560
Use a constant to ensure a value that shouldn't change, doesn't.

