简体   繁体   English

关于Java常数的困惑

[英]Confusion about constants in Java

I'm in the middle of studying java programming and I'm a bit confused about constants. 我正在学习Java编程,对常量有点困惑。 From what I've read so far constants are final and cannot be reassigned a new value unlike variables. 从目前为止我所读的常量是常量,不能像变量那样重新分配一个新值。 However, when I'm using the Calender class there is the set method which seems to change a constant field. 但是,当我使用Calender类时,有一个set方法似乎改变了一个常量字段。 For example: 例如:

Calendar cal = Calendar.getInstance();
System.out.println("The year is " + cal.get(Calendar.YEAR));
cal.set(Calendar.YEAR, 2001);
System.out.println("The year is " + cal.get(Calendar.YEAR));

If the Calendar.YEAR field is declared final in the Calendar class then why am I able to change it to another value using the set method? 如果Calendar.YEAR字段在Calendar类中声明为final ,那么为什么我可以使用set方法将其更改为另一个值?

Calendar.YEAR is just a constant saying which logical field you want to set within the calendar. Calendar.YEAR只是一个常数,它表示您要在日历中设置哪个逻辑字段。

The aim was to avoid having an API with 目的是避免使用带有

setYear
setDay
setMonth
...

In retrospect, I'd say this was a spectacularly bad idea - along with most of the rest of the design of java.util.Calendar and java.util.Date . 回想起来,我认为这是一个非常糟糕的主意-连同java.util.Calendarjava.util.Date的其余大部分设计一样。

So this call: 所以这个电话:

cal.set(Calendar.YEAR, 2001)

doesn't change the value of Calendar.YEAR ... it changes some other (private) field within the Calendar object. 不会更改Calendar.YEAR的值...它会更改Calendar对象内的其他(私有)字段。

Calendar.set(Calendar.YEAR, xx) changes the value of the year (for the calendar instance), not the value of Calendar.YEAR ( which is 1 ), see the javadoc Calendar.set(Calendar.YEAR, xx)更改年份的值(对于日历实例),而不是Calendar.YEAR的值( 1 ),请参阅javadoc

NB: it's easy to make a short program to verify that. 注意:编写一个简短的程序来验证这一点很容易。

Actually calendar.set(blah, blahVal) is not updating the first params (Calendar.YEAR) , it update the value which is internally maintain by the calendar, for example if you give cal.set(Calendar.YEAR, 2001) Calendar.YEAR is a constant which is index of an array. 实际上, calendar.set(blah, blahVal)不会更新第一个参数(Calendar.YEAR) ,它会更新(Calendar.YEAR)内部维护的值,例如,如果您给定cal.set(Calendar.YEAR, 2001)日历。 YEAR是一个常数,它是数组的索引。 that array contain the specified value. 该数组包含指定的值。 that value is update dramatically. 该值会急剧更新。

Calendar.YEAR is just an int which is defined in Constant Field Values , see the docs : Calendar.YEAR只是在“ 常量字段值”中定义的int ,请参阅docs

public static final int YEAR

It's not changing constant field, this int defines what field should be changed, see the source code : 它不是在更改常量字段,此int定义应更改哪个字段,请参见源代码

1196    public void set(int field, int value)
1197    {
1198        if (isLenient() && areFieldsSet && !areAllFieldsSet) {
1199            computeFields();
1200        }
1201        internalSet(field, value);
1202        isTimeSet = false;
1203        areFieldsSet = false;
1204        isSet[field] = true;
1205        stamp[field] = nextStamp++;
1206        if (nextStamp == Integer.MAX_VALUE) {
1207            adjustStamp();
1208        }
1209    }

Take a look at the docs Calendar#set to better understand what it does. 查看docs Calendar#set可以更好地了解它的作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM