What will you build

You will see build a small application that will use several constructors and the keyword this.

What You'll Learn

What will you need

All Java classes where the developper didn't implement a constructor have a automatic generated constructor that is called the default constructor.

This constructor doesn't exist anymore if the developper did implement a constructor, even if the constructor implemented has several parameters.

Several other programming languages have the same principle.

This default constructor has no parameters.

public class Student{

	public String returnUniversityName(){
		return "Cardiff university"; 
	}
}

The class to test this Student class.

public class TestStudent{

	public static void main (String[]args){
		Student student = new Student();
		String uni = student.returnUniversityName();
		System.out.println("univeristy name is "+uni);
	}
}

As you can observe in the above example, no constructor have been implemented in the Student class, however, it is still possible to instantialise an instance of this class, and we initialise it with invoking the default constructor that takes no arguments.

The name of the constructor should be exactly the same than the name of the class, and it is case sensitive.

It should, if the developper respect the writing convention of Java always start with an Upper Case, as the name of the Class should always start with an upper Case.

You should never have a method (other than the constructor) that is the same name of the class, it will works but it is really confusing.

The constructor doesn't return anything even void, so the signature of the constructor contains only the accessor (a constructor can be private or protected or default but the scope will be reduced), the name of the constructor and between parentheses the parameters of the constructor.

In the following example, I am defining two constructors, one that have two parameters and will initialise the instances variables: nameStudent and nameUniversity.

The second one, I redefine the default constructor (as now this default constructor doesn't exist anymore) and give some default value to the two instances variables.

In addition, in the class Student, I am implementing two methods that will return the variable universityName and another method that will return the nameStudent. These two methods return variables of type String.

public class Student{
	
	String nameStudent;
	String nameUniversity;
	
	public Student(String nameStudentA, String nameUniversityA){
		nameStudent = nameStudentA;
		nameUniversity = nameUniversityA;
	}
	
	public Student(){
		nameStudent = "Helene";
		nameUniversity = "Cardiff University";
	}

	public String returnUniversityName(){
		return nameUniversity; 
	}
	
	public String returnNameStudent(){
		return nameStudent; 
	}
}

As usual I am testing this class in another class defined below.

public class TestStudent{

	public static void main (String[]args){
		Student student = new Student();
		String uni = student.returnUniversityName();
		String nameStudent = student.returnNameStudent();
		System.out.println("My name is "+ nameStudent+ " and I am studying in "+uni);
		
		Student studentTwo = new Student("Claude", "Cam university");
		String uni2 = studentTwo.returnUniversityName();
		String nameStudent2 = studentTwo.returnNameStudent();
		System.out.println("My name is "+ nameStudent2+ " and I am studying in "+uni2);
	}
}

Taking the example above, we will change the name of the variable that are declared in the parameters of the constructor, and use the "this" to specify which variable we are using.

public class Student{
	
	String nameStudent;
	String nameUniversity;
	
	public Student(String nameStudent, String nameUniversity){
		this.nameStudent = nameStudentA;
		this.nameUniversity = nameUniversityA;
	}
	
	public String returnUniversityName(){
		return nameUniversity; 
	}
	
	public String returnNameStudent(){
		return nameStudent; 
	}
}

And as usual the class to test the code.

public class TestStudent{

	public static void main (String[]args){
		Student studentTwo = new Student("Claude", "Cam university");
		String uni2 = studentTwo.returnUniversityName();
		String nameStudent2 = studentTwo.returnNameStudent();
		System.out.println("My name is "+ nameStudent2+ " and I am studying in "+uni2);
	}
}

We can also use the "this" to invoke a constructor in the same class. In this example, the constructor that have no parameter calls the two-parameters constructor passing in the nameStudent and the nameUniversity as arguments, but always using the Helene and Cardiff university values (as they are hard-coded). The compiler will determines which constructor to call based on the number and the type of arguments.

The mechanism of calling a constructor inside another constructor is call "chaining the constructor".

In addition, This example, shows the overloading technique.

Overloading is a techniques that allows the developper to define several methods with the same name or several constructors in a same class. The condition being that the number of parameters or the type of variables should be different. The compiler will differentiates them at compile time.

public class Student{
	
	String nameStudent;
	String nameUniversity;
	
	public Student(String nameStudent, String nameUniversity){
		this.nameStudent = nameStudent;
		this.nameUniversity = nameUniversity;
	}
	public Student(){
		this("Helene","Cardiff university");
	}

	public String returnUniversityName(){
		return nameUniversity; 
	}
	
	public String returnNameStudent(){
		return nameStudent; 
	}
}

And as usual the class to test the code.

public class TestStudent{

	public static void main (String[]args){
		Student student = new Student();
		String uni = student.returnUniversityName();
		String nameStudent = student.returnNameStudent();
		System.out.println("My name is "+ nameStudent+ " and I am studying in "+uni);
		
		Student studentTwo = new Student("Claude", "Cam university");
		String uni2 = studentTwo.returnUniversityName();
		String nameStudent2 = studentTwo.returnNameStudent();
		System.out.println("My name is "+ nameStudent2+ " and I am studying in "+uni2);
	}
}

You can find the code for this example in https://git.cardiff.ac.uk/ASE_GROUP_2020/code_for_codelabs.git