Creating objects and Calling Methods


Now that, we have coded a class, we will now use it to create different Student objects. We do all of this in a test class. A test class isn’t a special type of class. It is just like our initial ‘Hello World ‘programs which had a main method that is called when the program is run. Recall that a program should have a main() method defined with the specifiers public, static and void if it is to be executable.

First, we will look into creating objects from classes. This is similar to how we declared variables of primitive data types and initialised them with values, for example: initialising the variable firstNumber with the integer 3. The same tasks are to be done here- Declaring the variable and initialising it with an object. But the additional task required here is the creation of objects. Since classes are reference data types, variables of a class type may not be initialised in a simple way as we have initialised integer and floating point variables. We create the object using the keyword new and then assign a reference of that object to the variable of the class type. Following is the general syntax of creating an object and assigning its reference to a variable of the corresponding class type.

< variable type – the class name > < variable/ object name > = new < call to constructor >;

The variable type that we specify here is the class name. the constructor is called by passing the required parameters if any. Suppose, we wish to create a Student object using the default constructor, then the following statement is used.

Student s1 = new Student ();

The parentheses were left empty since the default constructor does not require any arguments. However, if we wish to create a Student object by using a parameterised constructor, then we need to pass the appropriate arguments, separating them with commas. We can also separate the declaration of the Student variable from the initialisation with a Student object in the following way.

Student s1; s1 = new Student();

The above two statements are equivalent to the single statement which was earlier used to create the Student object s1. The following statements use the parameterised constructors of the Student class to create Student objects. We pass the required arguments by specifying them in the parentheses.

Student s2 = new Student ( “Sai”, 3 ); Student s3 = new Student ( “Gautham” , 4 , 98 , 100, 96);

The constructor that is to be called is decided by matching the argument list with the parameter lists of the constructors. In the first statement above, we have specified a String argument and an integer argument. We have a constructor that corresponds to these arguments. Hence that constructor is invoked, initialising, name to “Sai”, rollNumber to 3, and marks1, marks2 and marks3 to 0. The second constructor call above matches with the third constructor provided by us which requires five arguments. It is not just the number of arguments that is tallied for but also the order and type of these parameters. If we had replaced the first String with an integer value, or exchanged the first String and the last integer argument as shown below, we would have received a compilation error.

Student s3 = new Student (4 ,96, 98 , 100, “Gautham”); // incorrect

We can provide not just constants but also variables or expressions in the arguments list. They would be evaluated before the corresponding constructor is called. The third Student object s3 could have been created in the following way also:

int num = 96 ; Student s3 = new Student (“Gautham” , 4, num, num+2, 48+52);

The Student object that would result from this statement will identical to the one that was created earlier. Any variables or expressions in the arguments list are evaluated before they are passed to the constructor.

Now, we have three Student objects. Each one of them has their own copy of the instance variables which can be modified independently of others. Following is physical representation of these three objects.

Now that we have created the objects, we will look into how the variables and methods of these objects can be accessed. In order to access a variable or a method, we use the reference operator (.) in the following way:

s1.printDetails();

The above statement invokes the printDetails() method on the Student object s1. If the method requires arguments, we state them within the parentheses just as we had done in the case of parameterised constructors. Variables too are accessed in a similar way, except that the parentheses are not included. This is what separates the variables from methods and helps us to distinguish between the two. If the name variable was public we could have used the following statement to assign a String to the name variables of s1 in the following way:

s1.name=”Ravi”;

We can also print these values, as we would have printed a String variable if the variable was declared as public.

System.out.println(“Name is “+ s1.name);

But, for the current objects such access is denied since the variables were declared to be private which means that the variables are not accessible outside the class.

Now, we have the complete program below which creates three Student objects and invokes the printDetails() method on each of these objects.

public class StudentTest {

   public static void main ( String[] args ) {
      Student s1 = new Student () ;
      Student s2 = new Student ( “Sai”, 3 );
      Student s3 = new Student ( “Gautham” , 4 , 98 , 100, 96);
      System.out.println(“Student s1: “);
      s1.printDetails();
      System.out.println(“\nStudent s2: “);
      s2.printDetails();
      System.out.println(“\nStudent s3: “);
      s3.printDetails();
   }

}

When we run this program, we get the following output:

Student s1:
Roll Number: -1
Name: No name
Marks in first subject: -1
Marks in second subject: -1
Marks in second subject: -1

Student s2:
Roll Number: 3
Name: Sai
Marks in first subject: 0
Marks in second subject: 0
Marks in second subject: 0

Student s3:
Roll Number: 4
Name: Gautham
Marks in first subject: 98
Marks in second subject: 100
Marks in second subject: 96

Next : Get and Set Methods
Prev : The constituents of a Class

Author: , 0000-00-00