Predefined Classes in Java

MutatorAccessorObject

In the previous blog, we learned about Objects and classes in Java. If you want to know more about Arrays, visit Objects and Classes in Java. Class is a blueprint from which objects are made and an object is an instance of the class. In this blog, we will learn about the Predefined class used in Java. A predefined class name is a name that can be used as a class name. Class name has main() method. We will go through the Predefined Class in detail.

Predefined Classes

In the previous blog, we have seen that we can’t do anything without class. But some classes don't show the features of object orientation. For e.g. Math.sqrt(), suppose we want a square root of a number

e.g.

double sqRoot;
sqRoot = Math.sqrt(90);

In the above code, we have stored the result of square root in a variable called sqRoot. In order to display the square root of a number a method called sqrt() is used. The method sqrt() is directly called using class, which means that this method is a static method. Now you may be wondering why we have used static. So the reason is, we don’t care about the previous result stored in the variable, we just care about the result which will be stored when we call sqrt() method. We don’t manipulate the data, because we are only concerned about the result that we will get, so we use static. For example, if we want to find the sqrt of the no then we are not concerned about what the result was stored previously in that variable before calling sqrt(), we are just concerned about the result that we will get after the sqrt() is called.


Purpose of Math class

Now I have a question for you. Why have we used Math class here, we would directly use the static method and do the calculations. The answer to this question is that whenever sqrt function will be used it will give us the same result for all the programmers who may be using the method in their code. For example, if there are two programmers and they want to add two numbers that are float, now there may be a possibility that one programmer will take 2 digits after the decimal point and one programmer may take 4 digits. In order to give the same result the Math class is used where the result will be already fixed when we use the particular method from the Math class.


Object and Object Variables

When we want to work with objects then we have to construct(create) the object first, describe its state, and then use the methods on the object. We use the constructor to create a new instance(object) of the class. Constructor is the method where we create the objects and initialize the objects. For example, In the Date class, the object specifies the point in like i.e. December 31, 1999, 23:59:59 GMT.

Now you may be having a question why are we using the Date class rather than using the built-in type? For example, if we define the date in the built-in datatype in the format 31/12/1999, then the programmers can simply use the built-in datatype. But the issue will arise when the date will be in the format as day/month/year or year/month/day or month/day/year, etc. In this case, programmers will be helpless and won’t be able to do anything about the date. But on the other hand, if we use Date as the class then programmers can easily add their own classes whenever they want. To prove that the Date library was resigned twice due to some issues.

We have seen above that constructors are the methods used for creating objects and initializing them. Constructors have the same name(ConstructorEx) as the class name(ConstructorEx). For example, if we consider the example of the Date class, the Date class has the constructor with the same name as the classname i.e. Date. In order to get a clear understanding let's take another example where we will get a clear understanding of the Constructor. If we have the class called ConstructorsEx then the constructor will have the same name as the class i.e. ConstructorEx.

Syntax

class classname {
  ---------- -

  className() {  // Constructor
      ---------- -
  }
}

e.g.

class ConstructorEx {
  ---------- -

  ConstructorEx() {
      ---------- -
  }
}

We use the "new" keyword for creating objects of the class.

new ConstructorEx();

This statement creates the object of the ConstructorEx class. We can also pass the object to the method.

e.g.

System.out.println(new ConstructorEx());

We can also apply the method to the object. The ConstructorEx class will have the methods so we can call the method using the object.

e.g.

String value = new ConstructorEx().methodName();

In both the examples above we have used the object only once. In order to use the object we will store it in the variable, so whenever we will require the object then we will just use the variable.

e.g.

ConstructorEx value = new ConstructorEx();

The important difference between Object and Object Variable is - We will see it using the example below:

e.g.

ConstructorEx obj;

In the above example, we have defined the object variable obj which refers to the object of type ConstructorEx. Note that obj is not the object and does not even refer to the object. At this point, we cannot use any of the methods of the ConstructorEx class. If we try to call the method using the variable obj then we will get a compilation error. In order to do some operations we have to initialize the variable obj. There are two ways to initialize it. One way is to initialize the variable so that it will refer to the newly constructed variable.

e.g.

obj = new ConstructorEx();

The other way is to set the variable to refer to the existing object.

e.g.

ConstructorEx _obj = obj;

In the above examples, both the values refer to the same class i.e. ConstructorEx. Here one thing to note is that the object variable doesn’t contain an object, it refers to an object. The value of the object variable is the reference to the object i.e. ConstructorEx.

e.g.

ConstructorEx atrowel = new ConstructorEx();

The expression on the right of the assignment operator i.e. new ConstructorEx() makes an object of type ConstructorEx and its value is the reference to the object which is newly created. We can also set the object variable to null in order to indicate that it currently refers to no object.

e.g.

atrowel = null;
FeaturesC++ PointersJAVA Pointers
Null PointerNo null pointersContains null pointers
ImplementationConstructorEx* obj;ConstructorEx obj;
Uninitialized PointersDoesn’t give any error compile time, gives the error when the program is executedIt gives error compile time when there are uninitialized pointers

LocalDate Class

Date class can be used for displaying dates. Date class has a state i.e. point in time. In Date class time is represented by the number of milliseconds in positive or negative from the fixed point called “epoch” i.e. 00:00:00 UTC, January 1, 1970. UTC stands for Coordinated Universal Time which is the same as the most commonly used time i.e. GMT(Greenwich Mean Time). But if we want to manipulate the calendar information which human uses for date i.e. December 31, 1999, then the Date class is not suitable for it. The following description of the day follows the Gregorian calendar, which is the most commonly used calendar in many countries. The same day is represented differently in the Chinese or Hebrew calendar. For this reason, the Java library contains two separate classes. The Date class represents a point in time and the LocalDate Class represents the days in the calendar notation.

Whenever we create the class we use a constructor for creating the object, but LocalDate doesn’t use a constructor to construct the object. It uses factory methods rather than creating objects.

LocalDate.now();

The above statement constructs a new object which represents the date at which the object was created. We can also create an object for a particular date format like the year, month, and day.

LocalDate.of(2021,08,09);

If we wish to store the object in the object variable then we use

LocalDate atrowelDate = LocalDate.of(2021,08,09);

We can also find the year, month, and date using the following methods

int atrowelYear = atrowelDate.getYear();
int atrowelMonth = atrowelDate.getMonth();
int atrowelDay = atrowelDate.getDay();

Mutator and Accessor


Mutator Method

We use mutator method to store or change the value of the instance variables of the class. Setters are the mutator methods.

e.g.

public void setAtrowelValue(String atrowelValue) {
  this.atrowelValue = atrowelValue;
}

Use

MutatorAndAccessorEx atrowelObject = new MutatorAndAccessorEx();
/* To set the trowel value in the atrowelObject object */
atrowelObject.setName("Java Shortkicks");

Accessor Method

Accessor method is used to get the value stored in the private instance variable of the class. Getters is the accessor method. This method returns the variable value.

e.g.

public String getAtrowelValue() {
  return atrowelValue;
}

Use

MutatorAndAccessorEx atrowelObject = new MutatorAndAccessorEx();
String atrowelName;
/* To get the atrowel name in the atrowelObject object.*/
atrowelName= atrowelName.getAtrowelValue();

Core Java Tutorial

Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.