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.
/** 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. */
class loader, so static variables and static initializers System.out.print(Order.result + “ “) System.out.print(Order.result + “ “) u u
instance variables and instance initializers new Order(); result = “u” -> result += “c”, result += “r” -> ucr new Order(); result = “ucr” -> result += “c”, result += “r” -> ucrcr
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.