Can we write a method in java without return type like C programming?
class ReservedWords1
{
public static void main(String[] args)
{
System.out.println("main method");
m1();
}
static m1()
{
System.out.println("m1 method");
}
}
/*
Output :- CE[invalid method declaration; return type required]
Explanation :- If a method doesnot return anything then we should declare that method with
"void" return type. In Java, return type is mandatory but in C language return
type is optional and default return type is "int".
*/
{
public static void main(String[] args)
{
System.out.println("main method");
m1();
}
static m1()
{
System.out.println("m1 method");
}
}
/*
Output :- CE[invalid method declaration; return type required]
Explanation :- If a method doesnot return anything then we should declare that method with
"void" return type. In Java, return type is mandatory but in C language return
type is optional and default return type is "int".
*/
0 comments: