• Tidak ada hasil yang ditemukan

invoke one-parameter constructor with value zero

N/A
N/A
Protected

Academic year: 2024

Membagikan "invoke one-parameter constructor with value zero"

Copied!
29
0
0

Teks penuh

(1)

6001104-3 Structured Programming

L. Rafika Maaroufi

(2)

Chapter4: Constructor

&‘this’ reference in Java

(3)

Outline

Constructors

‘this’ keyword

(4)

Constructor

(5)

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

(6)

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).

(7)

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 . . . }

(8)

Example of Constructor

A

Same name

(9)

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.

(10)

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;

(11)

Example:

a simple constructor that simply sets the dimensions

of each box to the same values.

(12)

Example (cont.)

Keyword new requests memory from the system to store an object

(13)

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

(14)

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; }

(15)

Overloaded Constructors

(Example 1/2)

(16)

Overloaded Constructors

(Example 2/2)

(17)

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

(18)

Example: Parameterized

Constructors

(19)

Example: Parameterized

Constructors (cont.)

(20)

“this” keyword

(21)

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

(22)

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 }

(23)

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.

(24)

Example1:Using ‘this’ keyword to refer

current class instance variables (2/4)

(25)

Example1:Using ‘this’ keyword to refer current class instance variables (3/4)

parameters (formal

arguments)

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.

(26)

Example1:Using ‘this’ keyword to refer

current class instance variables (4/4)

(27)

“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.

(28)

Example2: Using this() to invoke

current class constructor

(29)

References

Java: How to Program, 9e, Dietel and Dietel , Pearson 0273759760

Data Structures and Algorithms in Java, 6th edition, by M.T. Goodrich and R.

Tamassia. John Wiley and Sons, Inc., ISBN:

0-471-73884-0

Java The Complete Reference Ninth Edition

Referensi

Dokumen terkait