what is constructer?what are the type of constructer with
example.

Answer Posted / prasad

A constructor is a method.it used to custom intialization
for an object.constructor name and class name is same.
consructor should not return any value not even void also.
A constructor will Executed at the time of initialization of
an object.
Different types of constructors:
--------------------------------
1)default constructor
2)parametarized constructor
Ex:


class sum{
int a,b;//instance variables
sum(int a,int b){
System.out.printl("sum of two numbers="+(a+b));
}
}
class Example{
psvm(String s[])throws Exception{
sum s=new sum(10,10);
}
}
o/p:sum of two numbers=20
/*here the constructor will be executed at the time of
creating the object*/

Is This Answer Correct ?    0 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the content reside in Use case Document?

1635


What is an api in java?

490


What is the java api?

496


What is the difference between jar and executable jar?

476


What is a cookie in java?

516






What is property file in java?

479


Do I need to install jre if I have jdk?

477


What is dao in java?

505


What is jdk for netbeans ide?

509


What is lazy loading in jpa?

444


What is mdb in java?

509


How do I start netbeans?

485


What is stateless and stateful in java?

494


What if the main method is declared as private?

472


What is @override annotation in java?

491