Java Reference
In-Depth Information
class Test
{
int i;
Test () {
i = 0;
This constructor illustrates
explicitly the initialization of
property values to their default
values as would occur if we had used
no constructor or included Test() {}
}
int get () {
return i;
}
}
As with methods, you can define multiple overloaded constructors to provide
optional ways to create and initialize instances of the class. (We discuss over-
loading of constructors and methods in more detail in Chapter 4.)
3.4 Class instantiation
Let's use the following class for our explanation of instantiation:
class Test
{
int i;
double x;
Test (int j, double y) {
i = j;
double x = y;
}
int getInt () {
return i;
}
double getDouble () {
return x;
}
double calculate () {
return i*x;
}
}
Search WWH ::




Custom Search