Method declaration in Java
Why method is necessary
It is necessary for operation on data in the
present in class
format for the method
type methodename (parameter)
{
Body of method
}
Example:
class A
{
int c,x,y;
void cal(int x,int y) //IT IS YOUR METHOD
{
c=x+y;
System.out.println(+c);
}
}
class Method //here you can take any class name
{
public static void main(String[] args)
{
A obj=new A(); //CREATING OBJECT FOR CLASS A
obj.cal(12,2); //ACCESSING METHOD
}
}
Here in above program we make to classes method
Is our main class for our main method
And class A is our second class
In which we take method cal
There is necessary to create the object of the class A
In our main class( here class method)
That’s why we create object of the class A in main class
A obj=new A();
A is our class name
Obj is user defined you can take
Any name or word here
In class A we declare method
void cal(int x,int y)
for accessing this method from main class
obj.cal(your parameter )
is require to declare in main class
Why method is necessary
It is necessary for operation on data in the
present in class
format for the method
type methodename (parameter)
{
Body of method
}
Example:
class A
{
int c,x,y;
void cal(int x,int y) //IT IS YOUR METHOD
{
c=x+y;
System.out.println(+c);
}
}
class Method //here you can take any class name
{
public static void main(String[] args)
{
A obj=new A(); //CREATING OBJECT FOR CLASS A
obj.cal(12,2); //ACCESSING METHOD
}
}
Here in above program we make to classes method
Is our main class for our main method
And class A is our second class
In which we take method cal
There is necessary to create the object of the class A
In our main class( here class method)
That’s why we create object of the class A in main class
A obj=new A();
A is our class name
Obj is user defined you can take
Any name or word here
In class A we declare method
void cal(int x,int y)
for accessing this method from main class
obj.cal(your parameter )
is require to declare in main class
No comments:
Post a Comment