Constructors in Java

Java Constructor

Constructors in Java

If an variable is declared inside the class outside the methods then such kind of variables are called “instance variables”.If an instance variable is declared and not initialised then it will automatically initialised with default value of that variable type.If we donot want the default value then we can initialised the instance variable either at the time of declaration or inside the constructor.

A constructor can be considered as the special method and its purpose is to initialised the instance variables.

Rules about the construction:

–>The name of the constructor and the name of the class must be same.
–>A constructor should not contain any return type not even void.
Note:If we specify a return type to a constructor.it is valid it is considered as a method.
–>A constructor can be invoke only during object creation.
–>A constructor can be invoke only one time for each object.
–>A constructor can contain parameters.Based on parameters passed to a constructor they are classified into 2 types.

1.Zero parameterized constructor/Default constructor:

If a constructor doesnot contain any parameter then it is called as “zero parameterized constructor”.

Eg: class <class name>{
<class name>() {
}
}

2.Parameterised constructor:

If a constructor contains same parameters then it is called as parameterised constructors.

Ex:class <class <class Name>{
<class Name>(parameter) {
}
}

–>A parameterised constructor can contain any no.of parameters and any type of parameters.If multiple parameters are available then support them by comma,
–>Every class will contain a constructor whether a orogrammer specifies it or not.If the programmer doesnot specify any constructor then compiler will provide zero parameterized constructor.

Note:The compiler will never provide parameterised constructors whether we specify a constructor or not whether we invoke a constructor or whether we create a object or not definitly a class will contain atleast one constructor mandatory.

Eg: class Test{
int x;
Test (){
x=22;
}
public static void main(String[]ar){
Test t=new Test();
System.out.println(t.x);
}
}

–>We csn imnitialise the instance variable at the time of declaration or by using a constructor.
–>If we initialise the instance variable at the time of declaration or by using zero parameterised constructors all the objects will contain same valuebut if we want every object to contain different value then initialise the instance variable by using parameterised constructors.

Ex:class Student{
int rollno;
student(int x){
roll no=x;
}
void show(){
System.out.println(rollno);
}
public static void main(String[]ar){
Student s1=new student(11);
s1.show();
Student s2=new student(12);

this keyword:

this keyword can we used for accessing the instance members.(variables and methods)of a class.
–>If there is no confusion between the instance and local variables then specified this keyword is optional.If we dont specify this keyword then the compiler will automatically place this keyword.
–>If this is a confusion between instance variables and local variables then we can avoid confusion by placing “this keyword”before the variables to that it refers to instance variable.

Instance variable:

–>If a variable is declared inside the class ,outside the blocks(members,constructors)then the variable is called”instance variable”.
–>The memory for the instance bariables will be allocated during the object creation and the memory will be allocated in heap memory.The memory for the instance variable will be allocated one time for each object that is every object will contain it’s own copy of “instance variables”.
–>The instance variables are specific to the objects they belong to and there not dependent on other objects.

Ex:class Student{
int rollno;
this.rollNo=rollNo;
}
void show(){
System.out.println(this.rollNo);
}
public static void main(String[]ar){

Student s1=new Student(123);
Student s2=new Student(234);
Student s3=new Student(345);

s1.show();
s2.show();
s3.show();

}
}

Follow me on social media: