adspace


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 java api?

999


Can we extract main method from another class?

1035


What is the first argument of the string array in main method?

1031


What is the locale class?

1027


what is reflection api? How are they implemented?

1106


Are jvm’s platform independent?

983


What if I write static public void instead of public static void?

938


What is the resourcebundle class?

1013


How does java handle integer overflows and underflows?

1116


What do you understand by casting in java language? What are the types of casting?

924