+ 1

how to declare a const in java ??

i don't find the const lesson in this tutorial

15th Mar 2017, 12:53 AM
Elghozi Nasreddine
Elghozi Nasreddine - avatar
8 odpowiedzi
+ 3
put final in front of variable for example final int HELLO = 2;
15th Mar 2017, 12:55 AM
Mark Lesniak
Mark Lesniak - avatar
+ 3
yes you can but kind of defeats the purpose of a constant you want to use constants for things like tax or a persons name so might as Well initialize right away but java lets you do whatever you want really lol
15th Mar 2017, 1:28 AM
Mark Lesniak
Mark Lesniak - avatar
+ 2
it is good style to caps lock the final variable
15th Mar 2017, 12:55 AM
Mark Lesniak
Mark Lesniak - avatar
+ 2
is that a question or a comment? lol
15th Mar 2017, 1:24 AM
Mark Lesniak
Mark Lesniak - avatar
+ 1
final varType varName = varValue; The keyword final does the trick. Also check out enums, they can be useful for for example if you wanna check what day it is, with enum you could do Day.SUNDAY. Try to keep them uppercase, and they are usually static.
15th Mar 2017, 1:37 AM
David Koplik
David Koplik - avatar
0
if i declare a const not initialized (final int x;) i can put a value in this const after declare
15th Mar 2017, 1:14 AM
Elghozi Nasreddine
Elghozi Nasreddine - avatar
0
question 😁
15th Mar 2017, 1:26 AM
Elghozi Nasreddine
Elghozi Nasreddine - avatar
0
ok, thank's
15th Mar 2017, 1:29 AM
Elghozi Nasreddine
Elghozi Nasreddine - avatar