In this article, based on chapter 3 of OCA Java SE 7 Programmer I Certification Guide, author Mala Gupta discusses default constructors.
In this article, based on chapter 3 of OCA Java SE 7 Programmer I Certification Guide, author Mala Gupta discusses default constructors
August 18, 2012
Constructors are special methods defined in a class that create and return an object of the class in which they’re defined. Constructors have the same name as the name of a class and they don’t specify a return type, not even a void. Constructors come in two flavors: user-defined constructors and default constructors. In this article, based on chapter 3 of OCA Java SE 7 Programmer I Certification Guide, author Mala Gupta discusses default constructors.
Constructors are special methods defined in a class that create and return an object of the class in which they're defined. Constructors have the same name as the name of a class and they don’t specify a return type, not even a void. Constructors come in two flavors: user-defined constructors and default constructors. In this article, based on chapter 3 of OCA Java SE 7 Programmer I Certification Guide, author Mala Gupta discusses default constructors.
As you may know, a constructor is used to create an object. What happens if you don’t define any constructor in a class? The following is an example of class
Employee
that doesn’t define a constructor:class Employee { #A
String name; #A
String age; #A
} #A
#A No constructor is defined in class Employee
You can create objects of this class in another class (class
Office
), as follows:class Office {
public static void main (String args[]) {
Employee emp = new Employee(); #A
}
}
#A Class Employee doesn't define a constructor, but
#this code compiles successfully
In this case, which method creates the object of the class
Employee
? Figure 1 shows what happens when a class Employee
is compiled that doesn’t define any constructor. In the absence of a user-defined constructor, Java defines a default constructor. This constructor doesn’t accept any method arguments. It calls the constructor of the base class and assigns default values to all the instance variables. Figure 1. When the Java compiler compiles a class that doesn’t define a constructor, the compiler creates one for it.
What happens if you add another constructor to the class
Employee
? class Employee {
String name;
String age;
Employee (int newAge, String newName) { #A
name = newName; #A
age = newAge; #A
System.out.println ('User defined Constructor'); #A
} #A
#A User-Defined Constructor
In this case, on recompilation, the Java compiler will notice that you’ve defined a constructor in class
Employee
. Hence, it won’t add a default constructor to it, as shown in figure 2.Figure 2. When a class with a constructor is compiled, the Java compiler doesn’t add a default constructor to it.
In absence of a no-argument constructor, the following code will fail to compile:
class Office {
public static void main (String args[]) {
Employee emp = new Employee(); #A
}
}
#A code won't compile
Exam Tip Java defines a default constructor if and only if you don’t define any constructor. If a class doesn’t define a constructor, the compiler will add a default, no-argument constructor to the class. But if you modify the class later by adding a constructor to it, Java compiler will remove the default, no-argument constructor that it initially added to the class.
Summary
We covered default constructors. Default constructors are defined by Java in case a user doesn’t define any constructor in a class. It’s interesting to note that you can modify and add a constructor to a class after the Java compiler has automatically generated a default constructor for you. But, on recompilation, the Java compiler removes the automatically generated constructor.
Here are some other Manning titles you might be interested in:
Unit Testing in Java Lasse Koskela | |
Making Java Groovy Kenneth Kousen | |
Play for Java Nicolas Leroux and Sietse de Kaper |
Manning Publications Co. is a publisher of computer books for professionals.
DIGITAL JUICE
No comments:
Post a Comment
Thank's!