We are currently writing the series on OCAJP certification exam. We offer OCAJP 7, OCAJP8, OCPJP 6 and OCPJP 7 for programmers. Keep checking our blog for interesting tips on certification topics.
We also wrote about how to prepare to take the OCAJP exam and how to prepare to take the OCPJP certifications. These posts can be very helpful if you’re looking for guidance in preparing for certification exams.
This post is about the OCAJP exam goal “Create and overload constructors; impact on default constructors”. You will be tested in the exam regarding default constructors as well as constructors with return types. You will be able understand the different syntaxes for overloading constructors by the end of this article.
What are Constructors in Java and how do they work?
Constructors are blocks of code that look similar to methods that are called when an object instance is created. A constructor is different from a method in that it doesn’t have an object return type. The name of the constructor must match the name the class. A method, on the other hand, has a return type.
Here are some characteristics of a builder:
Constructor is a method that has exactly the class name and no return type.
At the time of object creation, Constructor will need to be called.
The constructor for the object you are creating will be called when you create it.
If you type return type to constructor, it will become a normal method, but not a constructor.
Example:
public class Test Test()System.out.println(“oca”);//prints ocapublic static void main(String[] args) Test t =new Test();When you are creating Test class object, the corresponding constructor will be called. It prints oca
The above example shows that Test is the class name and Test is the constructor name.
The constructor name must match the class name exactly.
Compiler gives error if constructor name is different from class name. This happens because compiler thinks you are supposed to write method, but the return type is missing.
Take a look at the following example to see how you can understand this point
Example:
public class Test test() //compile time error at this lineSystem.out.println(“oca”);public static void main(String[] args) Test t =new Test();If you type return type for constructor, it becomes a normal method but not a constructor.
Take a look at the following example to see how you can understand this point
Example:
public class Test void Test()System.out.println(“oca”);public static void main(String[] args) Test t =new Test();If you run above program, you don’t get any output.Because there is return type for constructor, so it is a normal method.
You must call the method explicitly to execute it.
You can give four access modifiers to constructor – public, protected, private, default, and private – without specifying.
If you give public the constructor, you can create an object for that class from any class in any package.
Protected to constructor allows you to create objects for the same class from all classes in the same package, as well as from sub-classes of other packages.
If default is given to constructor (none is specified), you can create an object for that class from any class of the same package, but not from other classes.
If you give private to constructor you can create an object for that class within the same class. You can’t create objects from other classes or packages.
A constructor is used for initializing instance variables
Example:
public class Test private int value1;private int value2;Test(int value1, int value2) this.value1 = value1;this.value2 = value2;public static void main(String[] args) Test t = new Test(7, 8);System.out.println(t.value1 + ” ” + t.value2);// prints 7 8In the above example this is used to call instance variables of class.You can omit this if instance variable name and constructor parameter name are different
Default Constructor
Java inserts a constructor with no parameters if you don’t create one. This constructor is called Default Constructor.
Let’s take an example of how Java inserted constructor looks.
Example:
//The below program is what you typedpublic Class Test public stat void mainString[] argumentsTest ();}//new TestThe following program is what you see at the time of compilationpublic Test() Super ();}public dynamic void mainString[]argsTest ();}}Observe how the above program looks like, How Java inserted constructor}
The constructor does not have parameters.
Access modifiers of constructor and classes are the same.
Super() is a statement that calls super class No argument constructor.
Below is the program that illustrates super() statement.
Example:
{public class Test1 extends Test2 public static void main(String[] args) Test1 t = new Test1();class Test2 Public class Test1 extends test2 public stat void main(String[] argos) Test1 T = new Test1 class Test2