Take instructor-led Live class on Java Tutorial at :
A constructor is used in the creation of an object.
In the previous we created a class and different things with in a class like variables, methods. In order to use the variables and methods of the class we need to create an object.
You can consider class as blue print and using that blue print you create many kinds of objects.
How the objects are created using their blueprint?
It is created using constructor.
As the name suggests it actually constructs the objects according to the class.
A constructor is a special method that has no return type. When you declare a method it should have a return type either it should be an integer, float or any data type depending on the logic of the program. You use void for method which does not return anything.
But constructors do not have any return type as a normal method. So constructor is a special method with no return type.
Must have same name as the class it is in.
E.g.: If you have a class with name “Class1” then constructor should also be named as “Class1”.
Constructors can have access specifiers. If use public then that constructor will be accessed publically.
When a variable is declared without any access specifier then it can be taken as “default” which means that the classes present in the same package can be access that variable.
When we have two or more constructors with same name and different type of arguments then we say that we are overloading the constructor. This is what we call as constructor overloading.
If we have different names overloading does not happen.
Hence overloading of a constructor takes place if and only if:
All constructors defined for a class should have same name.
They should have different type of arguments.
They might also differ in the number of arguments and order of arguments passed.
Constructor overloading gets executed at run-time.
When we create we use “new” operator because it helps in allocating memory for the object.
What happen if I have two constructors with same name and with no parameters?- it returns an error
When we create an object of a class, you can use only one constructor of that class when constructor overloading is present in class.
If you want to use the other constructor then we have create a different object of that class and use it.
Hence you can use only one constructor of the class having many constructors for an object of that class.
Do we need to initialize all the instance variables of the class in the constructor? –No. If some of the instance variables are not initialized in the constructor then those variables will be assigned default values.
Java Syntax for Loop Constructs:
Loop means that it will do the things again and again. So when you want to do something repeatedly we use loops. There are different types of loop.
Do while loop
For each loop
It allows the code to be executed repeatedly.
This loop is typically used when the number of iterations is known.
Iteration means the number of times the loop should be executed.
For Loop working:
“for” is k
This repeats until the condition becomes false.
The difference between do-while loop and while loop is:
In while loop first the condition is evaluated and if condition is true then only the statements in the while loop will execute. If the condition is false then the statements in while loop will not execute.
But if you consider the reverse is happening. i.e first the statement in the loop will be executed and then condition is evaluated. Even if the condition is true or false the statements are getting executed at least once.
In general in a group of nested loops, which loop is executed the most number of times?-the innermost loop.
Please write back to us at email@example.com or call us at +91-8880862004 for more information.