Hey guys
.
Explain the work of return statement in Java
Answers
Answered by
0
return statement is used to return something
as like in this example
public class Program
{
static int x; // try to make the x variable being private: and get the error message mean of private static :
public static void main(String[] args) {
System.out.println(X.fun()); // try to call as this fun(): why it's required to call the static variable and method so good practice to call the static variable and method with class name.
}
}
class X{
static int fun(){
Program.x++; // try to replace the Program and seen what happend: answer why it is required to call static variable and method with class name :
return Program.x;// return the value of x
}
}
when the method fun is call then it's return the value of x:: for returning something we use return keyword and the return type before the method name as int is return type of fun method
as like in this example
public class Program
{
static int x; // try to make the x variable being private: and get the error message mean of private static :
public static void main(String[] args) {
System.out.println(X.fun()); // try to call as this fun(): why it's required to call the static variable and method so good practice to call the static variable and method with class name.
}
}
class X{
static int fun(){
Program.x++; // try to replace the Program and seen what happend: answer why it is required to call static variable and method with class name :
return Program.x;// return the value of x
}
}
when the method fun is call then it's return the value of x:: for returning something we use return keyword and the return type before the method name as int is return type of fun method
Answered by
5
Hii....
It does not need to contain a return statement, but it may do so. In such a case, a return statement can be used to branch out of a control flow block and exit the method and is simply used like this: return; If you try to return a value from a method that is declared void , you will get a compiler error.
HOPE IT'S HELP YOU
#BABU
It does not need to contain a return statement, but it may do so. In such a case, a return statement can be used to branch out of a control flow block and exit the method and is simply used like this: return; If you try to return a value from a method that is declared void , you will get a compiler error.
HOPE IT'S HELP YOU
#BABU
Similar questions