0%

JavaSE constructor

Default Constructor

default constructor = default non-arguments constructor = Java-created constructor

  • There can be no user-defined constructors in the class. In that case, a default constructor was created. We don’t have opportunity to use this() in our constructors.
  • Default constrctor written by compiler only appears when there is no user-defined constructor.

Overloading Constructor

  • Constructor Chaining
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
Overloaded constructors often call each other. One common technique
is to have each constructor add one parameter until getting to the
constructor that does all the work. This approach is called
constructor chaining. In this example, all three constructors are
chained.
*/

public class Mouse {
private int numTeeth;
private int numWhiskers;
private int weight;

public Mouse(int weight) {
this(weight, 16); // calls constructor with 2 parameters
}

public Mouse(int weight, int numTeeth) {
this(weight, numTeeth, 6); // calls constructor with 3 parameters
}

public Mouse(int weight, int numTeeth, int numWhiskers) {
this.weight = weight;
this.numTeeth = numTeeth;
this.numWhiskers = numWhiskers;
}

public void print() {
System.out.println(weight + " " + numTeeth + " " + numWhiskers);
}

public static void main(String[] args) {
Mouse mouse = new Mouse(15);
mouse.print();
}
}
  • this(…) may only be called as the first line of a constructor.
  • this(…) only calls user-defined constructors.

Order of Initialization

  1. superclass
  2. static variable declarations and static initializers
  3. instance variable declarations and instance initializers
  4. the constructor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//What is the result of the following?

public class Order {
static String result = "";

{
result += "c";
}

static {
result += "u";
}

{
result += "r";
}
}

public class OrderDriver {
public static void main(String[] args) {
System.out.print(Order.result + " ");
System.out.print(Order.result + " ");
new Order();
new Order();
System.out.print(Order.result + " ");
}
}

Result:
u u ucrcr
order of initialization:

  1. class loader, so static variables and static initializers
    System.out.print(Order.result + “ “)
    System.out.print(Order.result + “ “)
    u u
  2. instance variables and instance initializers
    new Order();
    result = “u” -> result += “c”, result += “r” -> ucr
    new Order();
    result = “ucr” -> result += “c”, result += “r” -> ucrcr

An Exam Question

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Which of the following are true? (Choose 2)

A. this() can be called from anywhere in a constructor.

B. this() can be called from any instance method in the class.

C. this.variableName can be called from any instance method in the class.

D. this.variableName can be called from any static method in the class.

E. You must include a default constructor in the code if the compiler does not include one.

F. You can call the default constructor written by the compiler using this().

G. You can access a private constructor with the main() method.

Answer:

C, G.

Option A, B: this() can only be called from a constructor in the
same class. this() may only be called as the first line of a
constructor.

Option C: this.variableName can be called from any instance method
to refer to an instance variable.

Option D: It cannot be called from a static method because there is
no instance of the class to refer to.

Option E, F: The default constructor is only written by the
compiler if no user-defined constructors were provided. Since there
can be no user-defined constructors in the class if a default
constructor was created, it is impossible for option F to be true.

Option G: Since the main() method is in the same class, it can call
private methods in the class.

Note:
The instance variables with final must be initialized in the initialization or at the declaration moment or in the constructor. Once initialized, they cannot be changed.

Reference