Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
A constant is a variable that can never change.
A value that isn't supposed to change shouldn't.
Constants are variables that can never change.
In this lesson, you will define a static final constant.
What is a constant, as I just said twice, a value that can never change?
Why do we use constants to ensure values that shouldn't change, don't?
And why are constant static, because they belong to the class, their value doesn't change regardless
of the object.
A field is not static because it has different values for each object.
In other words, each field belongs to a particular object.
A constant is the same across every object, regardless of the object, every employee works for the
same company, so a constant belongs to the class.
So why are constants final, final means you can never update the variable, a constant is a value that
should never change.
So the state of the constant needs to be final.
Inside employed out Java, you'll notice that I'm plainly writing the company name Java Stars, this
is an important value because it affects our code.
Imagine I accidentally change it here, but here I leave it.
This is bad because now everything is inconsistent and I'm vulnerable to getting bugs never play the
right an important value, you might change it by mistake.
Instead, use a constant.
So here, instead of rewriting the same value over and over.
I'm going to create a static final string.
Constant company name Java stars.
This is the naming convention for constants, every letter is uppercase and words are separated by an
underscore.
This information is in your cheat sheet.
But what's important is that now, instead of writing the same value over and over, I can keep referencing
the constant.
And if you ask me, this is a lot cleaner and it's a lot safer.
Should the constant be private, if there's no reason to make it public, then yes.
The constant is final, so I'm not worried about the color accidentally changing it, the color could
try, but it's not possible.
Here, I'll show you.
Java just won't let them see.
So we could leave it as public, but there isn't a reason to make it publicly available outside the
class, so it's good practice.
And honestly, even if you want to expose the value for whatever reason, it's good practice to just
give it a go getter that returns a copy of that value.
You might be wondering why do we need a GETER that returns a copy of the constant value of the constant
is final.
There's no point, it's just good practice.
If you decide not to include a getter and just make the constant public, that's fine, it's honestly
just a matter of opinion.
OK, now we're going to open up the movie star solution.
Looking at the movie class, can you guess what needs to be a constant.
The String Values DVD and Blu ray, what we did here by rewriting DVD and Blu ray over and over is extremely
bad practice.
We were basically playing with fire.
Can you imagine if I misspelled one of them, I would get so many bugs.
So what I'm going to do is make a constant for DVD private static Final String.
Format DVD.
Is equal to DVD.
And a constant for Blu ray.
Format Blu ray is equal to Blu ray.
Now, I can refer to each constant where I need to.
All right, let's recap a constant, the static, because it belongs to the class, it does not belong
to an object, a constant is final because it should never change.
Use a constant to ensure a value that shouldn't change, doesn't.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.