Java Programming: 14 - Constants

preview_player
Показать описание
In this tutorial I explain how to make constants in Java by using the final modifier on variables.

By convention, constants are capitalized with words separated by underscores.

You usually want to make constants static because they typically do not change values across different instances. Using the static modifier also allows you to use the variable without creating any instances.

It is also generally safe to make your constants have public visibility, since applying the final modifier makes the variable read-only.
Рекомендации по теме
Комментарии
Автор

Great explanation. That should be all I need to know about constants... for now...

firippumartinezu
Автор

brilliant...soothing voice that explained some of my java pain away...

warrenbrandt
Автор

i really like the way you pass on knowledge....you have a talent on teaching it seems..

Loukas_Anastasiadis
Автор

Final ensures that the value of the variable can't be change once it is assigned.

Static ensures that regardless of how many instances there are, there will only ever be one variable in memory that all instances share.

Making something both static and final is ideal for a constant, since there's no need to have multiple copies of it in memory corresponding to each instance. It also allows for static access to the variable if it is public (no instance needed).

CodeMonkeyCharlie
Автор

THANKS A LOT brother, your video saves my life :P

illesszilagyi
Автор

good job thanks it is very comprehensible for me even though I am not a native speaker

coderavecmdschool
Автор

Thank you for such an easy-to-understand explanation!!

prim
Автор

Thank you! It was very easy to understand!

parislove
Автор

If a reference is final, you can't change that reference.

final Person p1 = new Person();
Person p2 = new Person();
p1 = p2; // This fails because p1 is final and we're trying to change what p1 refers to

You could, however, say:
p2 = p1; // This works, because p2 is not final, and can refer to any Person object

final has no effect on working with the object and its data (for object/reference types).

CodeMonkeyCharlie
Автор

I don't understand why you made the Width and Height static, why couldn't you have just left it with the Final association

KIDZTAz
Автор

I didn't understand NUM-ENEMY ....
What it meaning?

Thegamer-ypqq
Автор

can i go straight from these tutorials to android progamming?
can i make an Array of Labels with an undefined number of labels?

IshaiHaviv
Автор

Java has updated
ALL YOU NEED TO DO IS TYPE
*final int HEIGHT;*
*final int WIDTH;*

yassine
Автор

The 'static' modifier and the creation of instances is still a bit fuzzy to me. The use of a constant and the creation of several instances seems contradictory. Can you please give an example, where you would leave out the 'static' modifier and still use 'final'?
Thanks for the great vids. Cheers.

jackjacobsen
Автор

Sir Is this compulsory to write constant variable name in upper case? Thank you in advance

saurabhsharma
Автор

I followed everything except for declaring a reference type as final

I'm unclear what you meant by...

"P1 is a final reference. It means the value in P1, which is a reference, is final. And what that means, is that you can never say that P1 equals a different Person, but you can access and change the data of the P1 object it refers to. All that is final is the reference itself, not necessarily the data inside the object that the reference points to."

Can you please clarify or give an example?

psyclonesmith