There are many values in the real world which will never change. A square will always have four sides, PI to three decimal places will always be 3.142, and a day will always have 24 hours. These values remain constant. When writing a program it makes sense to represent them in the same way - as values that will not be modified once they have been assigned to a variable. These variables are known as constants.
现实世界中有许多永远不变的价值观。 一个正方形将始终具有四个边,PI到小数点后三个位将始终为3.142,一天将始终具有24小时。 这些值保持不变。 编写程序时,以相同的方式表示它们是有意义的-因为一旦将值分配给变量,这些值将不会被修改。 这些变量称为常量。
将变量声明为常量 ( Declaring a Variable As a Constant )
In declaring variables we showed that it’s easy to assign a value to an int variable:
int numberOfHoursInADay = 24;
We know this value is never going to change in the real world so we make sure it doesn’t in the program. This is done by adding the keyword modifier
我们知道这个值在现实世界中永远不会改变,因此我们确保它不会在程序中出现。 这是通过添加关键字修饰符来完成的
final
In addition to the
final
keyword you should have noticed that the case of the variable name has changed to be uppercase as per the standard Java naming convention. This makes it far easier to spot which variables are constants in your c
If we now try and change the value of
we will get the following error from the compiler:
The same goes for any of the other primitive data type variables. To make them into constants just add the
final
keyword to their declarat
Where to Declare Constants
As with normal variables you want to limit the scope of constants to where they are used. If the value of the constant is only needed in a method then declare it there:
{
return days * NUMBER_OF_HOURS_IN_A_DAY;
If it’s used by more than one method then declare it at the top of the class definition:
public class AllAboutHours{
public int calculateHoursInDays(int days)
return days * NUMBER_OF_HOURS_IN_A_DAY;
public int calculateHoursInWeeks(int weeks)
final int NUMBER_OF_DAYS_IN_A_WEEK = 7;
}
Notice how I’ve also added the keyword modifiers
private
and NUMBER_OF_HOURS_IN_A_DAY
. This means that the constant can only be used by its class (hence the public
constant if you want other classes to have access to it. The one instance<
Using the Final Keyword with Objects
It’s very important to realize that when it comes to objects, Java does not support constants as you might expect. If you assign a variable to an object using the
final
keyword it means the variable will only ever hold the reference to that object. It cannot be changed to reference another object. However, it does not mean that the contents of the object cannot cha
A Brief Note on the Const Keyword
You may have noticed in the reserved words list that there is a keyword called
Java language<
final
In addition to the
final
keyword you should have noticed that the case of the variable name has changed to be uppercase as per the standard Java naming convention . This makes it far easier to spot which variables are constants in your c
If we now try and change the value of
we will get the following error from the compiler:
The same goes for any of the other primitive data type variables. To make them into constants just add the
final
keyword to their declarat
Where to Declare Constants
As with normal variables you want to limit the scope of constants to where they are used. If the value of the constant is only needed in a method then declare it there:
{
return days * NUMBER_OF_HOURS_IN_A_DAY;
If it’s used by more than one method then declare it at the top of the class definition:
public class AllAboutHours{
public int calculateHoursInDays(int days)
return days * NUMBER_OF_HOURS_IN_A_DAY;
public int calculateHoursInWeeks(int weeks)
final int NUMBER_OF_DAYS_IN_A_WEEK = 7;
}
Notice how I’ve also added the keyword modifiers
private
and NUMBER_OF_HOURS_IN_A_DAY
. This means that the constant can only be used by its class (hence the public
constant if you want other classes to have access to it. The one instance<
Using the Final Keyword with Objects
It’s very important to realize that when it comes to objects, Java does not support constants as you might expect. If you assign a variable to an object using the
final
keyword it means the variable will only ever hold the reference to that object. It cannot be changed to reference another object. However, it does not mean that the contents of the object cannot cha
A Brief Note on the Const Keyword
You may have noticed in the reserved words list that there is a keyword called
Java language<