SEMrush

Core Java - Mutator and accessor methods with code examples

Store the object in a variable to use it later :
Date birthday = new Date();

There is an important difference between objects and object variables.

Object variables are the variable in which Objects are stored

Ex. Date birthday = new Date();

Here "birthday" is an object variable
and "Date" is an object

GregorianCalendar Class

The Date class has only a small number of methods that allow you to compare two points in

time. For example, the before and after methods tell you if one point in time comes before
or after another.

if (today.before(birthday))
 System.out.println("Still time to shop for a gift.");

The expression below :

new GregorianCalendar()

constructs a new object that represents the date and time at which the object was constructed.

construct a calendar object for midnight on a specific date by supplying year, month
and day:

new GregorianCalendar(1999, 11, 31)
new GregorianCalendar(1999, Calendar.DECEMBER, 31)

set the time with:

new GregorianCalendar(1999, Calendar.DECEMBER, 31, 23, 59, 59

What are Mutator and accessor methods ?

Calendar enable us to compute attributes, such as the date, weekday, month, or year, of a
certain point in time. To query one of these settings you use the get method of the
GregorianCalendar class. To select the item that you want to get, you pass a constant
defined in the Calendar class, such as Calendar.MONTH or Calendar.DAY_OF_WEEK:

GregorianCalendar now = new GregorianCalendar();
int month = now.get(Calendar.MONTH);
int day = now.get(Calendar.DAY_OF_WEEK);

You to change the state with a call to the set method, you can do it as shown below:

deadline.set(Calendar.YEAR, 2001);
deadline.set(Calendar.MONTH, Calendar.APRIL);
deadline.set(Calendar.DAY, 15);

convenience method to set the year, month, and day with a single call:

deadline.set(2001, Calendar.APRIL, 15);

Finally, you can add a number of days, weeks, months, etc., to a given date.

deadline.add(Calendar.MONTH, 3); // move deadline by 3 months

If you add a negative number, then the calendar is moved backwards.

The get method only looks up the state of the object and reports on it. The set and add methods modify the state of the object

Methods that change instance fields are called mutator methods and those that only access instance fields without modifying them are called accessor methods.

x accessor methods with the prefix get and mutator methods with the prefix set. For example, the GregorianCalendar class has methods getTime and setTime that get and set the point in time that a calendar object represents.

Date time = calendar.getTime();
calendar.setTime(time);

Dear friends, Watch this space for more interesting articles on Core Java :)

<Previous

Some useful Links:
Abstraction Method Overriding Method Overloading Instance Variables  Java Applets Pop ups and Alerts Absolute path Relative path Annotations Selenium Webdriver Browser Commands Absolute path Vs Relative path Selenium Webdriver Pop ups and Alerts Testng Annotations - part 1 Object Models in QTP - Part 1Agile Testing Methodology Extreme Programming and customer satisfaction Mobile testing - What are the Challenges in mobile testing & Strategies we can follow to deal with them , Crowdsource testing - CrowdtestingModel-based testing (MBT)Big Data TestingCloud TestingTDD Test Driven DevelopmentVerification vs ValidationSoftware Testing Interview Questions - Mock Test CSTE / ISTQBSoftware testing types , Risk Management

Comments