first of all this code won't compile since there is no import statement for the Calendar class. Assuming you might have missed it the explanation is that DAY_OF_WEEK is a constant(public final static field) in Calendar class. when you did c.DAY_OF_WEEK , the reference variable c is actually getting replaced into Calendar. so c.DAY_OF_WEEK is same as Calendar.DAY_OF_WEEK(you already know that we can access static variable both using Class name and the reference variable(which is a bad practice)). now DAY_OF_WEEK is of type int whose value is 7, SO 7 gets printed. check the values of constants from here http://docs.oracle.com/javase/6/docs/api/constant-values.html#java.util.Calendar.WEDNESDAY
In calendar class...
public final static int DAY_OF_WEEK = 7;
c.DAY_OF_WEEK = static Day of week int code in calendar class. (the one above)
c.get(Calendar.DAY_OF_WEEK) = get the day today using DAY_OF_WEEK int code.
You can also do c.get(7) because the integer 7 is the code for the DAY_OF_WEEK field of the calendar class but it's not advisable to do so because future updates of the JDK may update the number assigned to DAY_OF_WEEK field to a different number which may cause a lot of java programs to fail but if you use c.get(Calendar.DAY_OF_WEEK) whatever changes to the integer code of DAY_OF_WEEK will not affect your program.
The calendar field values can be set by calling the set methods. Any field values set in a Calendar will not be interpreted until it needs to calculate its time value (milliseconds from the Epoch) or values of the calendar fields. Calling the get, getTimeInMillis, getTime, add and roll involves such calculation.
which explains that while invoking get methods on Calendar instance, it calculates with the current date set in the Calendar instance.
So, the below code explanation is correct.
c.get(Calendar.DAY_OF_WEEK) = get the day today using DAY_OF_WEEK int code
Rodine Villar wrote:You can also do c.get(7) because the integer 7 is the code for the DAY_OF_WEEK field of the calendar class but it's not advisable to do so because future updates of the JDK may update the number assigned to DAY_OF_WEEK field to a different number
...and because it makes your code unreadable!
I can't beleive you just said that. Now I need to calm down with this tiny ad: