[JAVA] Why the get method is needed in the Calendar class

I'm new to Java. Haven't come to a conclusion? However, just for the sake of memo,

Introduction

I was wondering when I used the Calendar class.

Isn't there a get method?

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,,

I took a peek inside the Calendar class

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.

Impressions

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

Why the get method is needed in the Calendar class
How to get the class name / method name running in Java
What is the main method in Java?
When you get lost in the class name
Why preventDefault is needed
Get the name of the test case in the JUnit test class
[Ruby] Method to easily get the receiver type .class
[Java] Get the date 10 days later with the Calendar class
What is the pluck method?
What is the initialize method?
Get date without using Calendar class
[Java] Get date information 10 days later using milliseconds in the Date class
Why are class variables needed? [Java]
[Spring Boot] Until @Autowired is run in the test class [JUnit5]
Call the super method in Java
Error when the member of Entity class used in SpringWebFlux is final
If the HTTP method is not specified, the browser side requests with GET
When the bean class name is duplicated
What is a class in Java language (3 /?)
Get the result of POST in Java
Spring Autowired is written in the constructor
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
How to get the date in java
Get the error message using the any? method
[Java] How to use the Calendar class
Sort by multiple fields in the class
[Java] Get the date with the LocalDateTime class
Part of the class called from the class under test in JMockit Mock method memo
Call a method of the parent class by explicitly specifying the name in Ruby
Get the class name and method name of Controller executed by HandlerInterceptor of Spring Boot
Get the path defined in Controller class of Spring boot as a list
Class method
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
Cause of is not visible when calling a method of another class in java
[Java] Calculate the day of the week from the date (Calendar class is not used)
[Rails] Bootstrap's text-light cannot be inherited by the link_to method because the class is not written as an argument in the method.
[Order method] Set the order of data in Rails
What is Pullback doing in The Composable Architecture
How to get keycloak credentials in interceptor class
[Java] Handling of JavaBeans in the method chain
The order of Java method modifiers is fixed
The ruby version is managed in the .rbenv / version file
Java method call from RPG (method call in own class)
Get your version number in the Android app
Exception silence is the worst (why programmers silence exceptions?)
[Java] Something is displayed as "-0.0" in the output
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
When the project is not displayed in eclipse
Ebean.update () is not executed in the inherited model.
I want to get the value in Ruby
Get the value from the array and find out what number it is included in
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java