WHAT IS CONSTRUCTOR ?

Constructors are usually used to construct the object.That means Typically, constructors are used to initialize instance variable state,
Every class, including abstract classes, MUST have a constructor.
Two key points to remember about constructors are that they have no return type and their names must exactly match the class name.
each time an instance is created the constructor(same class and super class)is called
Every class has a constructor, although if you don't create one explicitly, the compiler will build one for you.

class Demo{
public static void main(String[] bhabani){
Demo d=new Demo();//this is the default constructor called
}}


PARAMETERIZED CONSTRUCTOR-
The constructor that can take parameters.
class Demo{
Demo(int x){return x}//this is the parameterized constructor
public static void main(String[] bhabani){
Demo d=new Demo(5);
}}
In the above we can not call the default constructor.(Demo d=new Demo();)

These are all legal constructor
Demo() { }
private Demo(byte b) { }
Demo(int x) { }
Demo(int x, int... y) { }

if we will write like this
class Demo{
int Demo(int x){return x}//this is the parameterised constructor
public static void main(String[] bhabani){
Demo d=new Demo();//this is default constructor
}}
Here the default constructor is called.
The Demo() looks like constructor but it is not because the return type is int.
So it a normal method.
We can call method Demo like this d.Demo(5);

Constructors can not be static or final or abstract.


Constructors can be overloaded,

class Demo{
Demo(int x){}
Demo(int x,int y){}
public static void main(String[] args){
Demo d1=new Demo(5);
Demo d1=new Demo(5,6);
}
}
INVOKING OVERLOADED CONSTRUCTORS THROUGH THIS()
class Demo{
Demo(int x){}
Demo(int x,int y){}
Demo(){this(5)}
public static void main(String[] args){
Demo d1=new Demo(5);
Demo d1=new Demo(5,6);
Demo d=new Demo();//this calls the this(5).
//that means it calls the Demo(int x)constructor
//if we will declare the constructor like this Demo(){this(5,6);}, then it will call //the Demo(int x,int y) Constructor
}}
CONSTRUCTOR CHAINING
-Every constructor invokes the constructor of its superclass with an (implicit) call to super(), unless the constructor invokes an overloaded constructor of the same class.

watch this code
class a
{
a(){System.out.println("this is a");}
};
class b extends a
{
b(){System.out.println("this is b");}
};
class c extends b
{
c(){System.out.println("this is c");}
};
class demo extends c
{
demo(){System.out.println("this is demo");}
public static void main(String[] args){
demo d=new demo();
}
};
The output is like this
this is a
this is b
this is c
this is demo
Remember first parent exist then child.

Suppose any superclass takes an argument(suppose a(int x)).
In this case there will be compilation error.
We know sumclass invokes the default superclass constructor by super() internally.
if your super constructor (that is, the constructor of your immediate
superclass/parent) has arguments, you must type in the call to super(), supplying
the appropriate arguments.
class a{
a(int x){}}
class b extends a{
b(){super(5);/*constructor code here*/}}
class Demo(){
Demo d=new Demo();}

Then this code works.

RULES OF CONSTRUCTOR:
■ Constructors can use any access modifier, including private.
■ The constructor name must match the name of the class.
■ Constructors must not have a return type.
■ If you don't type a constructor into your class code, a default constructor will
be automatically generated by the compiler.
■ The default constructor is ALWAYS a no-arg constructor.
■ If you want a no-arg constructor and you've typed any other constructor(s)
into your class code, the compiler won't provide the no-arg constructor.
(In this case you can create your own no-arg constructor manually)
■ If you do type in a constructor (as opposed to relying on the compiler-generated
default constructor), and you do not type in the call to super() or a call
to this(), the compiler will insert a no-arg call to super() for you, as the very
first statement in the constructor.
■ A call to super() can be either a no-arg call or can include arguments passed
to the super constructor.
■ Abstract classes have constructors, and those constructors are always called
when a concrete subclass is instantiated.
■ Interfaces do not have constructors. Interfaces are not part of an object's
inheritance tree.
■ The only way a constructor can be invoked is from within another constructor.
AS
class Demo{
Demo(){}
Demo(String str){Demo();}}

PROPERTIES OF DEFAULT CONSTRUCTOR
if there is no constructor in your class then compiler puts a default constructor for your program.
So you can write "Demo d=new Demo()" without defining any constructor.
SOME POINTS TO REMEMBER:
■ The default constructor has the same access modifier as the class.
■ The default constructor has no arguments.
■ The default constructor includes a no-arg call to the super constructor
(super()).
■ this() can call the constructor of the same class.
■ The first line in a constructor must be a call to super() or a call to this() Because each of those calls must be the first statement in a constructor, you can't legally use both in the same constructor.
■ a constructor can never have both a call to super()and a call to this().

Can you synchronize a constructor ?
Constructors cannot be synchronized — using the synchronized keyword with a constructor is a syntax error. Synchronizing constructors doesn't make sense, because only the thread that creates an object should have access to it while it is being constructed.

4 comments: