WHAT IS A CONSTRUCTOR
"It is a special type of method same name as class name that determines how an object is initialized when it's created".
Like other methods, we can also define constructor Method in our java program but unlike other methods, we cannot call a constructor directly; Java called constructor automatically when an object has created. When we use new keyword to create an object of a class, java does three thing;
- Allocates memory for the object.
- Initialize that object instance variable, with their initial value or to a default.
- Call the constructor Method of the class.
If a class doesn't have defined any constructor method, we will still create an object of that class but we have to set instance variable or call other methods that object needs to initialize itself to that object afterward.
By defining constructor method in our own classes, we can set initial values of instance variable, call method based on those variable or call methods on other objects, or calculate initial properties of our object. We can also overload constructor, as we would regular methods, to create an object that has specific properties based on the argument we give to new.
BASIC CONSTRUCTOR
by defining a constructor looks like a regular method, with 2 basic difference.
- Constructor and class name are always same.
- It doesn't have any return type
For example, in the below table a simple class person, with a constructor that initializes it's instance variable based on the argument to new. The class also includes a method for the object to introduce itself, and a main() method to test each of these class.
class Person Person (String n, int a) void printPerson () public static void main(String args[]) Person p; p = new Person ("Ajab", 20); p = new Person ("Rizwan", 30); The output of the program is given below: Hi, I am Ajab. I am 20 years old. |
CONSTRUCTOR OVERLOADING
like other methods, constructor can also take different number and types of parameters, enabling us to create our objects with exactly the properties you want it to have, or for it to be able to calculate properties from different kinds of input.
For example, the MyRectone class in the given table creates a MyRectone Constructor and passing different parameter instead of creating different methods for the given arguments.
class MyRectone int x1 = 0; MyRectone ( int x1, int x2, int x2, int y2) this.x1 = x1; } MyRectone (Point topLeft, Point bottomRight) x1 = topLeft.x; } MyRectone ( Point topLeft, int w, int h) x1 = topLeft.x; } void printRect () System.out.print ("MyRectone: <" + x1 + ", " + y1); } public static void main (String args [] ) MyRectone rect; rect = new MyRectone (35,35,70,70); System.out.println ("Calling MyRectone with coordinates (15,15) (30,30)"); System.out.print (" Calling buildRect w/1 point (10,10),"); Output Calling MyRectone with coordinates 35,35 70,70: Calling buildRect w/1 points (15,15), (30,30): Calling buildRect w/1 point (10,10), width (50) and height (50): |
CALLING ANOTHER CONSTRUCTOR
Some constructor may be a superset of another constructor defined in your class; that is, they might have the same behavior plus a little bit more. Rather than duplicating identical behavior in multiple constructor Methods in our class, it makes sense to be able to just call that first constructor from inside the body of the second constructor. Java provides a special syntax for doing this. To call a constructor defined on the current class, use this form:
this (arg1, arg2, arg3 …..);
The arguments to this are, of course, the arguments to the constructor.
0 Comments