The constituents of a class


A class as already stated has three major components- variables, constructors and methods. A class may have all of these, some of these or none. We can create an empty class which doesn’t have variables, constructors or methods but however there would be no use of such a class. First, we shall look at the outer wrapping of the class which simply names the class. This is exactly similar to what we have already seen in our initial programs.
< access specifier > class < class name > {
}
Access specifiers that are permitted to be specified for a class are public and private. We may also leave it unspecified. We can simply write the word class and give a name to it. Access specifiers are used to define the domain in which an element is accessible. When we define a class a public, it means that the class is accessible from anywhere. On the other hand, defining a class as private restricts the accessibility to that particular program file. We shall look at specifiers again shortly. The word class is a keyword and finally the class name needs to follow the rules of identifiers. The braces serve the purpose of delimiting the class. Anything that appears within the braces is a part of the class. A group of statements enclosed in braces is known as a block. For now, we will move on to the Student class. We define the Student class in the following way.
public class Student {
   // variables
   // constructors
   // methods
}
At this point of time, it is necessary to understand that we do not have a main method here. We are of course free to include a main method but we shall test this Student class outside in a new class (or to be more clear- a program ) having a main method.
Now, we shall look at declaring the variables. Variables are declared by specifying the data type and the variable name. We may optionally state the access specifier also. Variables declared within the class are accessible to all the methods of the class. These variables characterise the object that would be instantiated from these classes. Following is the syntax for variable declaration.
< access specifier > < data type > < variable name >;
We may also give a value to it, which would be the default for objects instantiated from this class. But, we shall do it in a better way using constructors.
< access specifier > < data type > < variable name > = < value > ;
For now, here is the class Student with five variables.
public class Student {
   private int rollNumber;
   private String name;
   private int marks1;
   private int marks2;
   private int marks3;
}
We have applied the private access specifier to each of the variables. When we do so, these variables are accessible from within the class only. Any attempts to access them from outside the class will result in compilation errors. This can be better explained when we bring objects into the scene. We create Student objects from this class. After doing so, we cannot directly request access to these variables by using the object name and dot operator in the following way.
student1.marks1 = 100; // student1 is of type Student
Recall that we have used the dot operator several times earlier in the print() statement. The dot operator is used to access members of classes and objects. Earlier we have accessed the out variable of the class System. While now, we are trying to access the marks1 variable of a Student object, student1. This isn’t allowed since the marks1 variable was declared to be a private variable. If the variable was public, such a statement would have been valid. We shall see later on how we can create the object student1. We shall also see how these variables can be accessed or modified using get and set methods.
Now, we move on to constructors. Constructors are used to initialise an object. Constructors appear somewhat similar in syntax to methods but there are a few differences. There are two types of constructors- default constructors and parameterised constructors. Default constructors do not accept any parameters while parameterised constructors accept parameters. Recall that variables stated within the parentheses are referred to as parameters. These parameters have a data type and a variable name. We shall now first look into how we can provide a default constructor.
public Student ( ) {
   name = null;
   rollNumber = -1;
   marks1 = -1;
   marks2=-1;
   marks3=-1;
}
The header of a default constructor (the first line) consist of the access specifier and the constructor name. The constructor’s name should be the same as that of the class, which in this case is ‘Student’. Since this is a default constructor that doesn’t take any arguments, the parentheses are left empty. And then, as usual we start a block using an opening curly brace. We then initialise the variables to some default values. We have initialised the name to null. A reference data type when initialised to null indicates that the variable doesn’t refer to any object in memory. Recall that String is a reference data type (since it is class). And we have initialised the remaining integers to -1 to simply indicate that the Student wasn’t initialised with a proper value. You may, if you wish to do so, provide some other default values. The following default constructor would also be equally fine. But we have used the null and -1 default values as they reflect the scenario in a biter way.
public Student ( ) {
   name = “No name”;
   rollNumber = 0;
   marks1 = 2;
   marks2=3;
   marks3=10;
}
But, remember that a class cannot have more than one default constructor.
We now move onto to parameterized constructors. Parameterized constructors accept arguments which are data items that are used to initialise the variables of a class. Following shows a parameterised constructor for a class where the five variables are initialised with arguments that would be received by the constructor.
public Student ( String n, int rn, int m1, int m2, int m3) {
   name = n;
   rollNumber = rn;
   marks1 = m1;
   marks2 =m2;
   marks3= m3;
}
Since this is a parameterised constructor which receives arguments, the required values have been stated in the parentheses. We state the data type of the required argument and give a variable name. This particular parameterised constructor was specified with five parameters. Each of the parameters should be separated by a comma. The body of the constructor assigns the values received to the instance variables. Suppose we pass the values “Gautham”, 34,97,99,98 to this constructor; then “Gautham” would be stored in n, 34 in rn, 97 in m1 and so on. And then because of the statement name =n; name will be initialised with “Gautham”. In a similar manner, the other variables are also initialised with the values passed to the constructor. We shall see later on how we pass values to a constructor while creating an object using the new keyword.
We may define more than one constructor for a class. Suppose, we want to provide the facility where we should be able to create Student object by simply passing the name and roll number, and assign the marks to 0, we may do it with the help of another constructor as shown below.
public Student ( String n, int rn) {
   name = n;
   rollNumber = rn;
   marks1=0;
   marks2=0;
   marks3=0;
}
The process of providing more than one constructor to a class is known as constructor overloading. However, there are certain restrictions here which we shall see later on when dealing with constructor overloading. For now, we have declared the variables and provided three constructors. Now, let us provide a method which will print the details of a Student. A method definition would be similar to what we have already seen in our previous programs. The only difference would be that we will now omit the word static and also remove the String[] args parameter. Static was used earlier since there was a need for that main method to be called without crated an object of that class, be it HelloWorld or Addition class. The parameter String[] args too had a significance there about which we shall later on when dealing with command line arguments. String[] is a data type just like String and int and args was a variable name for the argument that would be passed to main. We will deal with them later on. For now, here is a method printDetails() which prints the details of the Student.
public void printDetails() {
   System.out.println(“Roll Number: “+ rollNumber);
   System.out.println(“Name: “+name);
   System.out.println(“Marks in first subject: “+marks1);
   System.out.println(“Marks in second subject: “+marks2);
   System.out.println(“Marks in second subject: “+marks3);
}
This method looks just like a constructor. The difference is that we have a return type and the name of the method need not be the same as that of the class. The return type here is void which means that the method will not return any value after execution. We shall see how to implement methods that return values in a few moments and how to use those returned values.
We have just started with the design of our Student class but it is still not completed. We haven’t provided a means to modify the values of the variables and there are no methods that print the total marks and average. However, this current version of the Student class is sufficient to build objects. Here is the first version of our Student class which has five instance variables; two constructors- one default and two parameterised and lastly a method to print details about the student.
The default constructor has been slightly modified to give default value for name as “No name” instead of null.
public class Student {

Private int rollNumber;
   private String name;
   private int marks1;
   private int marks2;
   private int marks3;

Public Student ( ) {
      name = “No name”;
      rollNumber = -1;
      marks1 = -1;
      marks2=-1;
      marks3=-1;
   }

Public Student ( String n, int rn, int m1, int m2, int m3) {
      name = n;
      rollNumber = rn;
      marks1 = m1;
      marks2 =m2;
      marks3= m3;
   }

Public Student ( String n, int rn) {
      name = n;
      rollNumber = rn;
      marks1=0;
      marks2=0;
      marks3=0;
   }

Public void printDetails() {
      System.out.println(“Roll Number: “+ rollNumber);
      System.out.println(“Name: “+name);
      System.out.println(“Marks in first subject: “+marks1);
      System.out.println(“Marks in second subject: “+marks2);
      System.out.println(“Marks in second subject: “+marks3);
   }

}

Author: , 0000-00-00