Constructor :
- It has the same name as class in which it resides.
- A Constructor initializes an object upon creation .
- Constructor doesn't have return type even void ,since it return the whole class .
- Automatic intialization is performed through the use of constructor , so we prefer constructor than method.
ex:
class addc {
public addc(int a,int b )
{
int c= a+b;
System.out.println(c);
}
}
class mdemo {
public static void main(String[ ] args)
{
addc a1 = new addc(12,8);
}
}
Method :
This is the general form of a method
type name (parameter-list) {
//body of method
}
ex:
class addc {public int addc(int a,int b )
{
int c= a+b;
return c; // Method can have return type
}
}
class mdemo {
public static void main(String[ ] args)
{
addc a1 = new addc( );
int d = a1.addc(12,8); // Getting the result throw by return
System.out.println(d);
}
}
Here, type specifies the type of data returned by the method .if the method does not return a value, its return type must be void.
__________________________________________________________________________
0 comments:
Post a Comment