A constructor looks more like
a method but without return type. Moreover, the name of the constructor and the
class name should be the same. The advantage of constructors over methods is
that they are called implicitly whenever an object is created. In case
of methods, they must be called explicitly. To create an object, the
constructor must be called. Constructor gives properties to an object at the
time of creation only. programmer uses constructor to initialize variables,
instantiating objects and setting colors. Constructor is equivalent to init()
method of an applet.
Default
Constructor – No Argument Constructor
A
constructor without parameters is called as "default constructor"
or "no-args constructor". It is called default because if the
programmer does not write himself, JVM creates one and supplies. The default
constructor supplied by the JVM does not have any functionality (output).
1
2
3
4
5
6
7
8
9
10
11
12
|
public class Demo
{
public Demo()
{
System.out.println("From default constructor");
}
public static
void main(String args[])
{
Demo d1 = new
Demo();
Demo d2 = new
Demo();
}
}
|
Output
screen of Demo.java
public
Demo()
"public" is the access specifier and
"Demo()" is the constructor. Notice, it does not have return
type and the name is that of the class name.
Demo d1 =
new Demo();
In the above
statement, d1 is an object of Demo class. To create the object, the
constructor "Demo()" is called. Like this, any number of objects can be created like d2 and for
each object the constructor is called.
Constructor Overloading
Just like method overloading, constructors also can be
overloaded. Same constructor declared with different parameters in the same
class is known as constructor overloading. Compiler differentiates which
constructor is to be called depending upon the number of parameters and their
sequence of data types.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class Perimeter
{
public Perimeter()
// I
{
System.out.println("From default");
}
public Perimeter(int x)
// II
{
System.out.println("Circle perimeter: " + 2*Math.PI*x);
}
public Perimeter(int x, int
y) // III
{
System.out.println("Rectangle perimeter: " +2*(x+y));
}
public static
void main(String args[])
{
Perimeter p1 = new
Perimeter(); // I
Perimeter p2 = new
Perimeter(10); //
II
Perimeter p3 = new
Perimeter(10,
20);
// III
}
}
|
Output
screen of Perimeter.java
Perimeter constructor is overloaded three times. As per the parameters, an appropriate constructor is called. To call all the three constructors three objects are created. Using this(), all the three constructors can be called with a single constructor.
this() with
Constructors
Suppose by
accessing one constructor, the programmer may require the functionality of
other constructors also but by creating one object only. For this, Java comes
with this(). "this()" is used to access one constructor from
another "within the same class". Depending on the parameters
supplied, the suitable constructor is accessed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class Perimeter
{
public Perimeter()
// I
{
System.out.println("From default");
}
public Perimeter(int x)
// II
{
this();
System.out.println("Circle perimeter: " + 2*Math.PI*x);
}
public Perimeter(int x, int
y) // III
{
this(100);
System.out.println("Rectangle perimeter: " +2*(x+y));
}
public static
void main(String args[])
{
Perimeter p3 = new
Perimeter(10,
20);
// III
}
}
|
Output
screen of Perimeter.java
In the code, creating object p3, the III constructor is accessed. From III, with "this(100)" statement, the II constructor is accessed. Again from II, the I is accessed without the statement "this()". As per the parameter supplied to this(), the appropriate or corresponding constructor is accessed.
Rules of
using this()
A few
restrictions exist for the usage of this().
- If included, this() statement
must be the first one in the constructor. You cannot write anything before
this() in the constructor.
- With the above rule, there
cannot be two this() statements in the same constructor (because both
cannot be the first).
- this() must be used with
constructors only, that too to call the same class constructor (but not
super class constructor).
"this" keyword
refers an object, to say, the current object. Observe the following
program and pay attention to this.salary.
public class Officer3
{
int salary;
public void
display(int
salary)
{
this.salary = salary;
}
public static
void main(String args[])
{
Officer3 o1 = new
Officer3();
o1.display(5000);
System.out.println(o1.salary); // 5000
}
}
Output
screen of Officer3.java
In the display()
method, local variable and the instance variable are same, salary. That
is, local variable clashes with instance variable. If "this"
is removed in display() method, o1.salary in the main() method
prints 0. "this" refers the object o1 as with o1
the display() method is called. Now we can make one rule, if a method
is called with an object, the instance variables inside the method are linked
with the object implicitly. Now, this.salary becomes o1.salary
internally and one memory location is created. If "this" is
removed, both salaries are treated as local variables and thereby the compiler
does not create any memory location. Use always "this" keyword
to differentiate a local from instance variable when they clash. C++ people,
call "this keyword" as "this pointer".
No comments:
Post a Comment