Method is a block of code. Its used to divide the large programs into small pieces. It is reusable.
Syntax
class class-name
{
public static void main(String[] args)
{
System.out.println("main method");
}
public static void method ()
{
System.out.println("method");
}
}
class class-name
{
public static void main(String[] args)
{
System.out.println("main method");
}
public static void method ()
{
System.out.println("method");
}
}
Uses of method
1.chunk programs.
2.reusable.
3.easy to debugging.
1.chunk programs.
2.reusable.
3.easy to debugging.
Method overload
Same method with different arguments is called method overload.
class bike
{
void fun()
{
System.out.println("method with out parameter :");
}
void fun(int a)
{
System.out.println("method with parameter : "+a);
}
public static void main(String[] args)
{
bike B=new bike();
B.fun();
B.fun(5);
}
}
{
void fun()
{
System.out.println("method with out parameter :");
}
void fun(int a)
{
System.out.println("method with parameter : "+a);
}
public static void main(String[] args)
{
bike B=new bike();
B.fun();
B.fun(5);
}
}
Output
method with out parameter :
method with parameter : 5
method with out parameter :
method with parameter : 5
Comments
Post a Comment