Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Final Keyword | Deep Java Structure
Java Extended
course content

Conteúdo do Curso

Java Extended

Java Extended

1. Deep Java Structure
2. Methods
3. String Advanced
4. Classes
5. Classes Advanced

bookFinal Keyword

How to prevent changing the value of a variable

For this purpose, Java has a keyword called final. Let's consider a code snippet where we use this keyword:

java

Main

copy
123456789101112
package com.example; public class Main { public static void main(String[] args) { int number = 10; final int finalNumber = 5; number = 15; finalNumber = 20; System.out.println(number); System.out.println(finalNumber); } }

You can see that in the code above, we have created two variables of type int, but one of them is marked with the final keyword. When we try to change the value of this variable, the compiler tells us about an error. Specifically, it indicates that we cannot modify a value that is marked with the final keyword.

What can we use final keyword for:

  • To create constants. A constant is a variable whose value cannot be changed after it has been initialized. In Java, constants are always declared using the final keyword;
  • To indicate that the value of a variable is final and should not be modified;
  • To prevent accidental modification of a final variable's value in our code.

Note

Just using the keyword final alone is not enough to create a constant. To create constants, the construct private static final is used. Additionally, all constants have uppercase names separated by underscores, for example:

private static final int NUMBER_OF_CHESS_PLAYERS = 2;

Tarefa

Here is the code where I create multiple variables and perform mathematical operations on them. However, I only perform these operations on certain variables. Your task is to add the final keyword before the variables that I do not modify in this code to prevent me from accidentally changing them later.

Note

It is not necessary to use final everywhere. Use this keyword only when you are certain that you will not need to change the value of the variable in the future. If you use this keyword where it is not needed, you may confuse yourself in your own code.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3
toggle bottom row

bookFinal Keyword

How to prevent changing the value of a variable

For this purpose, Java has a keyword called final. Let's consider a code snippet where we use this keyword:

java

Main

copy
123456789101112
package com.example; public class Main { public static void main(String[] args) { int number = 10; final int finalNumber = 5; number = 15; finalNumber = 20; System.out.println(number); System.out.println(finalNumber); } }

You can see that in the code above, we have created two variables of type int, but one of them is marked with the final keyword. When we try to change the value of this variable, the compiler tells us about an error. Specifically, it indicates that we cannot modify a value that is marked with the final keyword.

What can we use final keyword for:

  • To create constants. A constant is a variable whose value cannot be changed after it has been initialized. In Java, constants are always declared using the final keyword;
  • To indicate that the value of a variable is final and should not be modified;
  • To prevent accidental modification of a final variable's value in our code.

Note

Just using the keyword final alone is not enough to create a constant. To create constants, the construct private static final is used. Additionally, all constants have uppercase names separated by underscores, for example:

private static final int NUMBER_OF_CHESS_PLAYERS = 2;

Tarefa

Here is the code where I create multiple variables and perform mathematical operations on them. However, I only perform these operations on certain variables. Your task is to add the final keyword before the variables that I do not modify in this code to prevent me from accidentally changing them later.

Note

It is not necessary to use final everywhere. Use this keyword only when you are certain that you will not need to change the value of the variable in the future. If you use this keyword where it is not needed, you may confuse yourself in your own code.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3
toggle bottom row

bookFinal Keyword

How to prevent changing the value of a variable

For this purpose, Java has a keyword called final. Let's consider a code snippet where we use this keyword:

java

Main

copy
123456789101112
package com.example; public class Main { public static void main(String[] args) { int number = 10; final int finalNumber = 5; number = 15; finalNumber = 20; System.out.println(number); System.out.println(finalNumber); } }

You can see that in the code above, we have created two variables of type int, but one of them is marked with the final keyword. When we try to change the value of this variable, the compiler tells us about an error. Specifically, it indicates that we cannot modify a value that is marked with the final keyword.

What can we use final keyword for:

  • To create constants. A constant is a variable whose value cannot be changed after it has been initialized. In Java, constants are always declared using the final keyword;
  • To indicate that the value of a variable is final and should not be modified;
  • To prevent accidental modification of a final variable's value in our code.

Note

Just using the keyword final alone is not enough to create a constant. To create constants, the construct private static final is used. Additionally, all constants have uppercase names separated by underscores, for example:

private static final int NUMBER_OF_CHESS_PLAYERS = 2;

Tarefa

Here is the code where I create multiple variables and perform mathematical operations on them. However, I only perform these operations on certain variables. Your task is to add the final keyword before the variables that I do not modify in this code to prevent me from accidentally changing them later.

Note

It is not necessary to use final everywhere. Use this keyword only when you are certain that you will not need to change the value of the variable in the future. If you use this keyword where it is not needed, you may confuse yourself in your own code.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

How to prevent changing the value of a variable

For this purpose, Java has a keyword called final. Let's consider a code snippet where we use this keyword:

java

Main

copy
123456789101112
package com.example; public class Main { public static void main(String[] args) { int number = 10; final int finalNumber = 5; number = 15; finalNumber = 20; System.out.println(number); System.out.println(finalNumber); } }

You can see that in the code above, we have created two variables of type int, but one of them is marked with the final keyword. When we try to change the value of this variable, the compiler tells us about an error. Specifically, it indicates that we cannot modify a value that is marked with the final keyword.

What can we use final keyword for:

  • To create constants. A constant is a variable whose value cannot be changed after it has been initialized. In Java, constants are always declared using the final keyword;
  • To indicate that the value of a variable is final and should not be modified;
  • To prevent accidental modification of a final variable's value in our code.

Note

Just using the keyword final alone is not enough to create a constant. To create constants, the construct private static final is used. Additionally, all constants have uppercase names separated by underscores, for example:

private static final int NUMBER_OF_CHESS_PLAYERS = 2;

Tarefa

Here is the code where I create multiple variables and perform mathematical operations on them. However, I only perform these operations on certain variables. Your task is to add the final keyword before the variables that I do not modify in this code to prevent me from accidentally changing them later.

Note

It is not necessary to use final everywhere. Use this keyword only when you are certain that you will not need to change the value of the variable in the future. If you use this keyword where it is not needed, you may confuse yourself in your own code.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 1. Capítulo 3
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
some-alt