1

I'm a newbie learning Java currently being introduces do the switch/case construct. The book I'm reading uses the following example:

int eingabe = 256; 
final byte einKleinesByte = 2; 
final char einKleinerCharacter = 'c'; 
final short einKleinesShort = 500; 
switch(eingabe) {
case einKleinesByte: 
case einKleinerCharacter: 
case einKleinesShort: 

The explanation is that these 3 constant are defined with the help of the keyword final, which is essential, because variables cannot be used for the cases.

My question is, what is the difference between final byte einKleinesByte = 2; and byte EIN_KLEINES_BYTE = 2; as they are both defined as constants?

Thanks for the help!

  • if you define something like this: byte EIN_KLEINES_BYTE = 2; - it is not constant. Only key-word final can make it constant – Alex Rmcf Jun 23 '20 at 11:55
  • By `byte EIN_KLEINES_BYTE = 2`, did you mean `static final byte EIN_KLEINES_BYTE = 2`? – ZhekaKozlov Jun 23 '20 at 11:59
  • Thinking in Java (Fourth Edition) > Java's **final** keyword has slightly different meanings depending on > the context, but in general it says, "This cannot be changed." > > A constant is useful for two reasons: > 1. It can be a *compile-time constant* that won't ever change. > 2. It can be a value initialized at run time that you don't want changed. – Evan Knox Thomas Jun 23 '20 at 12:29

4 Answers4

1

byte EIN_KLEINES_BYTE is not a constant, it can be overwritten.

final byte EIN_KLEINES_BYTE can't be, that is a constant.

a final variable can not be reassigned. For primitive types, or for unchangeable types, this means that they are constant in value. For most objects, it doesn't. You can change the state (value) of those objects, but you can't change the object itself for another.

Just by putting a variable name in upper case, does not make it a constant.

EDIT: if you want a final variable to be the same for the entire application, you'll have to declare it as static, as well.

Stultuske
  • 9,296
  • 1
  • 25
  • 37
  • I think, what she actually wanted to ask is what the difference between `final byte einKleinesByte = 2` (defined as a local var) and `static final EIN_KLEINES_BYTE = 2` is. – ZhekaKozlov Jun 23 '20 at 11:57
  • @ZhekaKozlov check the last sentence of the question, the static-non static is not what the question is about – Stultuske Jun 23 '20 at 11:58
  • the question was about constants, not about class field access – Alex Rmcf Jun 23 '20 at 12:08
  • @AlexRmcf to quote the OP / Question itself: "My question is, what is the difference between final byte einKleinesByte = 2; and byte EIN_KLEINES_BYTE = 2; as they are both defined as constants?" No, the question is not about constants, it 's about the OP's (wrong) understanding of constants. – Stultuske Jun 23 '20 at 12:10
  • my comment was for @ZhekaKozlov – Alex Rmcf Jun 23 '20 at 12:15
  • 1
    I have mistaken the java convention of all caps (EIN_KLEINES_BYTE) to be able to define a constant value (blush). I'm at the very beginning, so I'm not yet at classes and class access etc :-) – Emiliyana Kalinova Jun 23 '20 at 12:15
  • @EmiliyanaKalinova, that was my first thought when i saw your question) – Alex Rmcf Jun 23 '20 at 12:18
  • @AlexRmcf my mistake :) – Stultuske Jun 24 '20 at 05:53
  • 1
    @ZhekaKozlov the keyword `static` is entirely irrelevant here. `final byte einKleinesByte = 2;`, defined as local variable, still is a compile-time constant. That’s why it is allowed as `case` label in a `switch` statement, as in the OP’s code, but also in annotations, to name another case where it matters. In contrast, without the `final` keyword it is not, even if you declare it as `static` variable. – Holger Oct 13 '20 at 09:44
0

The main difference between final variable and a constant (static and final) is that if you create a final variable without static keyword, though its value is un-modifiable, a separate copy of the variable is created each time you create a new object. Where a constant is un-modifiable and have only one copy through out the program.

thisisjaymehta
  • 614
  • 1
  • 12
  • 26
  • 1
    not entirely. it just means that the 'constant' would exist in a smaller scope. Also, just because a variable is declared final, doesn't automatically mean you can't change it's value. – Stultuske Jun 23 '20 at 11:56
  • @Stultuske "Where a constant is un-modifiable and have only one copy through out the program." - you are referring to static and final, right ? So if I understand correctly: final = multiple copies are possible if multiple objects are created. Will they all have the same value or can the value differ for the different objects? static final = there is only one constant existing and the value cannot be changed – Emiliyana Kalinova Jun 23 '20 at 12:22
  • That's what I meant – thisisjaymehta Jun 23 '20 at 12:25
  • Ups, yes, tagged the wrong user... this is going great :-D Thank you both! – Emiliyana Kalinova Jun 23 '20 at 12:30
  • Just one last thing - could the values of the final variable be different if multiple entities are created? – Emiliyana Kalinova Jun 23 '20 at 12:50
  • @EmiliyanaKalinova final means you can't re-assing the variable, it doesn't (always) mean you can't change it's value. so, depending on the context, in order to speak of a constant, it should be final (you can't change the variable for another variable), immutable (you can't change it's state/value) and public static (only one copy for the entire application – Stultuske Jun 23 '20 at 13:04
  • @Stultuske I think you cant even change the value of final variable. Try declaring `final int i = 0` and do `i++` it will give error. See here http://tpcg.io/SN7opjqe – thisisjaymehta Jun 23 '20 at 14:10
  • @EmiliyanaKalinova Yes values of the final variable can be different if multiple entities are created. – thisisjaymehta Jun 23 '20 at 14:16
  • @thisisjaymehta it has nothing to do with that. your example is a primitive type. if it is an instance of a class, which has setters, you can not assign a new instance to the variable, but you can change the state (value) through its setters. – Stultuske Jun 23 '20 at 16:21
  • @Stultuske I got it. So if we make a final object, then it will point to the same object once assigned and can not be referenced to some other object. But the parameters of that object can change. – thisisjaymehta Jun 24 '20 at 04:52
  • @thisisjaymehta yes. if it is a primitive type, or when it is an immutable type, that 'll somewhat make it a constant both as object and value, otherwise, it can be considered a constant, because it remains the same object, but the value can change – Stultuske Jun 24 '20 at 05:35
0

They are not both defined as constant. You can easily change the value of the byte EIN_KLEINES_BYTE. You are using the snake uppercase standard which is usually used to define constant but in reallity, there is nothing that stops you from changing the value of this variable. Only the keyworld final would prevent that.

// would work fine
int MY_CONSTANT = 2;
MY_CONSTANT = 3

// Will throw an error
final int myConstant = 2;
myConstant = 3

// recommended 
static final int MY_CONSTANT = 2
Nicolas
  • 8,077
  • 4
  • 21
  • 51
0

final simply means that the value, once assigned, can not be changed or reassigned

concerning constants, i would refer you to this question

fesieg
  • 467
  • 1
  • 4
  • 14