• Tidak ada hasil yang ditemukan

Constructors

Dalam dokumen Pelajari tentang Advanced topics in Java (Halaman 36-39)

Introduction to Objects

2.3 Constructors

private String author = "No Author";

private String title;

private double price;

private int pages;

private char binding = 'P'; // for paperback private boolean inStock = true;

}

Now, when an object is created, author, binding, and inStock will be set to the specified values while title, price, and pages will assume the default values. A variable is given a default value only if no explicit value is assigned to it. Suppose we create an object b with this:

Book b = new Book();

The fields will be initialized as follows:

•฀ author is set to "No Author". // specified in the declaration

•฀ title is set to null. // default for (String) object type

•฀ price is set to 0.0. // default for numeric type

•฀ pages is set to 0. // default for numeric type

•฀ binding is set to 'P'. // specified in the declaration

•฀ inStock is set to true. // specified in the declaration

We can do this, but we must first write an appropriate constructor, one defined with two String parameters.

The following shows how it can be done:

public Book(String a, String t) { author = a;

title = t;

}

Here are some important points to note:

A constructor for a class has the

•฀ same name as the class. Our class is called Book; therefore, the constructor must be called Book. Since a constructor is meant to be used by other classes, it is declared public.

A constructor can have zero or more parameters. When called, the constructor must be given

•฀

the appropriate number and type of arguments. In our example, the constructor is declared with two String parameters, a and t. When calling the constructor, two String arguments must be supplied.

The body of the constructor contains the code that would be executed when the constructor

•฀

is called. Our example sets the instance variable author to the first argument and title to the second argument. In general, we can have statements other than those that set the values of instance variables. We can, for instance, validate a supplied value before assigning it to a field.

We will see an example of this in the next section.

A constructor does not have a return type, not even

•฀ void.

If initial values are provided for instance variables in their declaration, those values are stored

•฀

before the constructor is called.

For example, suppose the class Book is now declared as follows:

public class Book {

private static double Discount = 0.25;

private static int MinBooks = 5;

private String author = "No Author";

private String title;

private double price;

private int pages;

private char binding = 'P'; // for paperback private boolean inStock = true;

public Book(String a, String t) { author = a;

title = t;

}

} //end class Book The statement

Book b = new Book("Noel Kalicharan", "DigitalMath");

will be executed as follows:

1. Storage is found for a Book object, and the address of the storage is stored in b.

2. The fields are set as follows:

author is set to "No Author"; // specified in the declaration title is set to null; // default for (String) object type price is set to 0.0; // default for numeric type

pages is set to 0; // default for numeric type binding is set to 'P'; // specified in the declaration inStock is set to true. // specified in the declaration

3. The constructor is called with arguments "Noel Kalicharan" and "DigitalMath"; this sets author to "Noel Kalicharan" and title to "DigitalMath", leaving the other fields untouched. When the constructor is finished, the fields will have the following values:

author "Noel Kalicharan"

title "DigitalMath"

price 0.0 pages 0 binding 'P' inStock true

2.3.1 Overloading a Constructor

Java allows us to have more than one constructor, provided each has a different signature. When several

constructors can have the same name, this is referred to as overloading the constructor. Suppose we want to be able to use the no-arg constructor as well as the one with author and title arguments. We can include both in the class declaration like this:

public class Book {

private static double Discount = 0.25;

private static int MinBooks = 5;

private String author = "No Author";

private String title;

private double price;

private int pages;

private char binding = 'P'; // for paperback private boolean inStock = true;

public Book() { }

public Book(String a, String t) { author = a;

title = t;

}

} //end class Book

Observe that the body of the no-arg constructor consists of an empty block. When the following statement is executed, the instance variables are set to their initial values (specified or default), and the constructor is executed. In this case, nothing further happens.

Book b = new Book();

Be warned that when we provide a constructor, the default no-arg constructor is no longer available. If we want to use a no-arg constructor as well, we must write it explicitly, as in the previous example. We are free, of course, to write whatever we want in the body, including nothing.

As a final example, we provide a constructor that lets us set all the fields explicitly when an object is created. Here it is:

public Book(String a, String t, double p, int g, char b, boolean s) { author = a;

title = t;

price = p;

pages = g;

binding = b;

inStock = s;

}

If b is a variable of type Book, a sample call is as follows:

b = new Book("Noel Kalicharan", "DigitalMath", 29.95, 200, 'P', true);

The fields will be given the following values:

author "Noel Kalicharan"

title "DigitalMath"

price 29.95 pages 200 binding 'P' inStock true

Dalam dokumen Pelajari tentang Advanced topics in Java (Halaman 36-39)