• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can a field like HOUR change if it is declared final (a constant)?  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm reading a beginner's Java book and came across the word "field", yet the term was never defined satisfactorily in the book. (I hate it when that happens.) I researched the API for a while and it seems to me that a field is simply a static final variable (...or a constant). If that's the case, how can the field HOUR of the Calendar class, for example, change if it is supposed to be final? I know that you can change the HOUR field with the add(field, value) method.

Is that because the field is not an actual variable but is referring to another place where the hour is stored? I am just rather clueless.

Thanks in advance!
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Victor Lu wrote:I know that you can change the HOUR field with the add(field, value) method.


You do? Can you show us a small piece of code which you think demonstrates that?
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The HOUR in class Calendar is a constant that it just used to indicate the hours field - it is not the hours field itself. When you use it in the add method, you're just using it to indicate that you want to change the hours field. You're not changing the constant itself.
 
Victor Lu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your responses!

@Paul

Calendar c = Calendar.getInstance();
c.add(Calendar.HOUR, 5);

@Jesper

I see! So "field" basically means "static variable"? And HOUR refers to another variable/field that actually stores the hour. So HOUR is marked "final" because it's always referring to that variable, which is private so it's not displayed in the API.

Is that accurate?
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Victor Lu wrote:
@Paul

Calendar c = Calendar.getInstance();
c.add(Calendar.HOUR, 5);

THis in no way proves your assertion that this changes the value of the HOUR constant.
 
Victor Lu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
THis in no way proves your assertion that this changes the value of the HOUR constant.


from the api:

add

public abstract void add(int field,
int amount)

Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules. For example, to subtract 5 days from the current time of the calendar, you can achieve it by calling:

add(Calendar.DAY_OF_MONTH, -5).


but Jesper has cleared the issue up nicely.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!