Record

Java is an object-oriented programming language which uses objects to organise items of data which should be stored together. Objects belong to classes which define the the fields that make up an object instance of that class. Fields can be modified by assigning a value to them or by invoking a method.

Java has a number of predefined classes, including the String class that we have just looked at. Programmers can also define their own classes. The relationship between an object and its class is similar to the relationship between a value and its type, but there is one important difference: an object reference can be null, but a value cannot.

The term “record” is not used in Java. A record is effectively the same as a Java object that has instance variables only, but no instance methods. Some other languages, such as Pascal,  support records. The C programming language uses the term “struct” to describe the same concept.

The fields in record are referred to by name and different fields in a record can be of different types. For example, the class Person could be defined as:

class Person {
String name;
int id_number;
Date birthday;
int age;
}

then an object of class Person could be considered to be a record with four fields. The field names are name, id_number, birthday and age. Note that the fields are of various types: String, int, and Date.

Consider a simple class for student data which could be implemented in Java as follows:

class Student {
public String studentid;
public String surname;
public String forename;
}

This is known as a class definition. The class identifier is Student and objects of this class will have three fields: studentid, surname and forename, allowing us to store these three related items of data together. A single object of this class might appear as shown below:

Student class

The object identifier points to the area in the computer’s memory where the three related items of data are to be found.

This object could be created by a short Java code fragment:

Student AnnieMcKelvie = new Student(); // create a new object
AnnieMcKelvie.studentid = "123456";    // assign a value to studentid
AnnieMcKelvie.surname = "McKelvie";    // assign a value to surname
AnnieMcKelvie.forename = "Annie"       // assign a value to forename

The first statement uses the default constructor for the class to declare a Student object. The remaining statements assign values to the fields.

Next: Array