0
I am confused in constructor...
Give an exmple and teach me.. Add what is the use of constructor there
4 Answers
+ 3
rules to use constructor
1).it's name must be the same as of class name
2).it must not have any return type
And working is very much similar to the function/method.
it is used to create instance of a class.
JVM always creates a default constructor for every class .
Constructor is of two types
1).default
2). parameterized
Ex:-
class A
{
A()//default constructor
{}
A(int b)// parameterized constructor
{}
//rest of the code.......
}
+ 1
Constructor is a special function used to initialise variables in a class.
it is defined as-
eg-
class_name(int var)
{
int a=var;
}
here we have passed a variable var and assigned it to a which is a member of Class class_name.
+ 1
Here is complete explanation about constructor: http://javabasictoadvanced.blogspot.co.id/2016/06/constructor.html
0
Thanku abhishek