java constructor
Whenever we are creating an object some part of the java program(code) will be executed automatically to perform initialization of the object, this part of the program is called "Constructor".
Hence the main objective of the constructor is to perform initialization of an object.
example 1.
class Employee
{
String name;
int eid;
Employee (String name, int eid)
{
this.name = name;
this.eid = eid;
System.out.println(name+"-----"+eid);
}
public static void main(String args[ ])
{
Employee e1 = new Employee("Dhoni",101);
Employee e2 = new Employee("Virat",102);
}
}
Output of the above program is:-
example 2. Java program to print the number of objects created for a class?
class Test
{
static int count= 0;
{
count ++;
}
Test ()
{
}
Test (int i)
{
}
Test (double d)
{
}
Test (String s)
{
}
public static void main(String args[ ])
{
Test t1 = new Test();
Test t2 = new Test(100);
Test t3 = new Test(105.5);
Test t4 = new Test("javabychetan");
System.out.println("Total Object created are : "+count);
}
}
Output of above program is:-
Rules for Creating Constructor:-
- Name of the class and name of the constructor must be same.
- Return type concept not applicable for the constructor. By mistake, if we are trying to declare the return type for the constructor then we will not get any compiler time error. Because compiler will treat it as a normal method.
- The only modifier applicable for constructor are public, private, protected, default. If we are trying to use any other modifier then we will get compile time error.
Default Constructor of Java
- The compiler is responsible for generating default constructor but not JVM.
- If we are writing any other constructor in our java program then the compiler will generate the default constructor.
- If we are writing at least one constructor then the compiler won't generate default constructor hence for every class in java can contain constructor. It may be generated by the compiler or created by programmer explicitly.
Structure of default Constructor
- It is always no-argument constructor.
- The access modifier of default constructor is exactly same as the access modifier of the class.
- It contains only one statement.
- It is a no-argument call to the super class constructor.
Note : The first line of every constructor should be either super or this and if we are writing anyother then we will get compile time error.
Let's See some compiler generated codes.
Conclusion 1.
We can take super () or this () only in the first line of constructor. if we are trying to take anyehere else then we will get compile time error.
Conclusion 2.
Within the constructor, we can take either super or this but not both simultaneously.
Conclusion 3.
We can take super () or this () only inside the constructor.If we are trying to use outside of constructor we will get the Compile-Time error.
No comments:
Post a Comment
Be the first to comment!
Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.
Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.
Regards,
JavaByChetan
Back To Home