6001104-3 Structured Programming
L. Rafika Maaroufi
Chapter4: Constructor
&‘this’ reference in Java
Outline
Constructors
‘this’ keyword
Constructor
Constructor
It can be tedious to initialize all of the
variables in a class each time an instance is created.
Java allows objects to initialize themselves when they are created
Each class you declare can provide a special method called a constructor that can be used to initialize an object of a class when the
Constructor
A constructor must have the same name as the class
e.g: when defining the Counter class, a
constructor must be named Counter as well.
Constructors cannot be static, abstract, or final.
The only modifiers that are allowed are those that affect visibility (i.e., public, protected, private, or the default package-level visibility).
Declaring a constructor
The general syntax for declaring a constructor in Java is as follows:
modifiers name(type0 parameter0 , . . . , typen−1 parametern−1)
{
// constructor body . . . }
Example of Constructor
A
Same name
Constructor
Java requires a constructor call for every object that’s created
To initialize the object
calls the corresponding class’s constructor the call is indicated by the parentheses after the class name.
The responsibility of the constructor method is only to initialize the state of the new instance.
Constructor
Constructors look a little strange because they have no return type, not even void.
Nor does the body of a constructor explicitly return anything.
When a user of a class creates an instance using a syntax such as Counter d = new
Counter(5);
The new operator is responsible for returning a reference to the new instance to the caller;
Example:
a simple constructor that simply sets the dimensions
of each box to the same values.
Example (cont.)
Keyword new requests memory from the system to store an object
Default Constructor
If no constructors are explicitly defined, Java
provides an implicit default constructor for the class, having zero arguments and leaving all instance
variables initialized to their default values.
public class MyClass {
public static void main (String[] args) {
MyClass obj= new MyClass();
}
…
… }
public class MyClass {
MyClass (){
}
public static void main (String[] args) {
MyClass obj= new MyClass();
}
…
… }
Compilation
Overloaded Constructors
Overloaded constructor is called based upon the parameters specified when new is
executed.
A class can have many constructors, but each must have a different signature,
each must be distinguished by the type and number of the parameters it takes.
e.g: public Counter( ) { }
public Counter(int initial) { count = initial; }
Overloaded Constructors
(Example 1/2)
Overloaded Constructors
(Example 2/2)
Constructors with Multiple Parameters
Sometimes you’ll want to initialize objects with multiple data items.
Example: we ask you to store the course name and the instructor’s name in a GradeBook object
public GradeBook( String courseName, String instructorName )
Calling the GradeBook constructor as follows:
GradeBook gradeBook = new GradeBook( "CS101
Example: Parameterized
Constructors
Example: Parameterized
Constructors (cont.)
“this” keyword
The Keyword this
‘this’ is a reference variable that refers to the current object
There are three common reasons why this
reference is needed from within a method body:
1. To store the reference in a variable, or send it as a parameter to another method that
expects an instance of that type as an
The Keyword this
2. To differentiate between an instance variable and a local variable with the same name.
Example:
3. To allow one constructor body to invoke another constructor body.
public Counter( ) {
this(0); // invoke one-parameter constructor with value zero
}
public Counter(int count) {
this.count = count; // set the instance variable equal to parameter }
Example1:Using ‘this’ keyword to refer current class instance variables (1/4)
Let's understand the problem if we don't use this keyword by the following example.
Example1:Using ‘this’ keyword to refer
current class instance variables (2/4)
Example1:Using ‘this’ keyword to refer current class instance variables (3/4)
parameters (formalarguments)
and instance variables are same.
So, we are using this keyword to
distinguish local variable and
instance variable.
parameters (formal arguments)
and instance variables are same.
So, we are using this keyword to
distinguish local variable and
instance variable.
Example1:Using ‘this’ keyword to refer
current class instance variables (4/4)
“this” keyword
If local variables(formal arguments) and instance variables are different, there is no need to use this keyword like in the following program.