I'm new to Java. Haven't come to a conclusion? However, just for the sake of memo,
I was wondering when I used the Calendar class.
The Calendar class has getter and setter methods that can retrieve and assign years, months, days, days of the week, and so on. For example, if you want to get the current year
import java.util.*;
class CalendarTest {
public static void main(String args[])
{
Calendar cld = Calendar.getInstance();
System.out.println("YEAR getter method →"+cld.get(cld.YEAR));
}
}
It is possible by using the and getter methods.
output
YEAR getter method → 2019
I have wondered here.
** Should I call cld.YEAR
? **When. Even if you look at the API reference, the access control level of the field YEAR
is public, so you can call it (if it is not public, the abovecld.get (cld.YEAR)
will not be usable either. ).
Anyway, I'll check it.
import java.util.*;
class CalendarTest {
public static void main(String args[])
{
Calendar cld = Calendar.getInstance();
System.out.println("YEAR getter method →"+cld.get(cld.YEAR)+",Field →"+cld.YEAR);
}
}
output
YEAR getter method → 2019,Field → 1
Hmm,? It seems that the YEAR field contains an integer of 1 instead of 2019. It seems that you can make various speculations, but let's check various things.
import java.util.*;
class CalendarTest {
public static void main(String args[])
{
Calendar cld = Calendar.getInstance();
for(int i = 2015; i<2020 ;i++){
cld.set(Calendar.YEAR,i);
System.out.println("YEAR("+i+")getter method →"+cld.get(cld.YEAR)+",Field →"+cld.YEAR);
}
}
}
output
YEAR(2015)getter method → 2015,Field → 1
YEAR(2016)getter method → 2016,Field → 1
YEAR(2017)getter method → 2017,Field → 1
YEAR(2018)getter method → 2018,Field → 1
YEAR(2019)getter method → 2019,Field → 1
Well, it seems that the field YEAR
always has an integer of 1.
So, when I checked the other fields, I found that the field MONTH
was always 2 and the field DAY_OF_MONTH
was always 5.
I thought, and when I checked the API reference, ...
Again, each field was declared a constant of type final
. Lack of confirmation,,
For the time being, I found that I need to use the get method as the title suggests. Is the field declared as a constant a serial number? (Check it out.
So what does the getter method return instead of the field? I checked the inside of the Calendar class. The getter and setter methods looked like this.
Calendar.java
class Calendar {
public int get(int field)
{
complete();
return internalGet(field);
}
protected final int internalGet(int field)
{
return fields[field];
}
final void internalSet(int field, int value)
{
fields[field] = value;
}
public void set(int field, int value)
{
if (areFieldsSet && !areAllFieldsSet) {
computeFields();
}
internalSet(field, value);
isTimeSet = false;
areFieldsSet = false;
isSet[field] = true;
stamp[field] = nextStamp++;
if (nextStamp == Integer.MAX_VALUE) {
adjustStamp();
}
}
}
Ignoring the details, there is a constant field such as YEAR
as a subscript (index) of the array called fields
, and it feels like assigning or retrieving a value to the element specified by the field.
Is it okay to put a value in each field normally (like public int YEAR = 2001
)? I couldn't get rid of that feeling.
I'm not sure where the benefits of having getter and setter methods are. (After all, I don't understand encapsulation or object orientation.
However, it seems easy to handle that each array does not have separate elements, but is grouped together like {[year], [month], [day], ...}. I felt it ^^
Studying Java, I don't know how to do it, and it's hard to make progress ...
Recommended Posts