And the method definition consists of a method header and a method body.
Now, Let’s understand how to create a user-defined Method in Java?
How to create a user-defined method in Java?
Syntax:
Now, Let’s understand how to create a user-defined Method in Java?
How to create a user-defined method in Java?
Syntax:
Here, a method named myMethod() is defined.
You can see three access modifiers – public, static and void before the function name.
1 The public keyword makes myMethod() method public. Public members can be accessed from outside of the class. To learn more, visit: Access modifiers in Java
2 The static keyword denotes that the method can be accessed without creating the object of the class.
3 The void keyword signifies that the method doesn’t return any value.
You can see three access modifiers – public, static and void before the function name.
1 The public keyword makes myMethod() method public. Public members can be accessed from outside of the class. To learn more, visit: Access modifiers in Java
2 The static keyword denotes that the method can be accessed without creating the object of the class.
3 The void keyword signifies that the method doesn’t return any value.
How to call a method/ method calling?
To call a method in Java, you have to write the method’s name followed by parentheses () and a semicolon ;
For using a method in a program, it should be called. There are two ways in which a method is called i.e., the method returns a value or it returns nothing.
The process of method calling is simple. When a program invokes a method, the program control automatically gets transferred to the called method. This called method returns control to the caller in these two conditions, they are namely:
- When the return statement is executed.
- When the control reaches the method ending closing brace.
To call a method in Java, you have to write the method’s name followed by parentheses () and a semicolon ;
For using a method in a program, it should be called. There are two ways in which a method is called i.e., the method returns a value or it returns nothing.
The process of method calling is simple. When a program invokes a method, the program control automatically gets transferred to the called method. This called method returns control to the caller in these two conditions, they are namely:
- When the return statement is executed.
- When the control reaches the method ending closing brace.
In this case,
1 When Java is executing the program code, it encounters the method in the code.
2 The execution then branches to the myFunction() method and executes code inside the body of the method.
3 After the execution of the code inside the method body is completed, the program returns to the original state and executes the next statement.
Let’s see a Java method in action by defining a Java class.
1 When Java is executing the program code, it encounters the method in the code.
2 The execution then branches to the myFunction() method and executes code inside the body of the method.
3 After the execution of the code inside the method body is completed, the program returns to the original state and executes the next statement.
Let’s see a Java method in action by defining a Java class.
Now let’s talk about the method parameters.
Method parameters
Parameters are specified after the method name in a class, inside parentheses. You can add as many parameters as you want but just separate them with a comma. Data can be passed to functions as a parameter. Actually, parameters act as variables inside the method.
Let’s take a look at an example to understand this.
Method parameters
Parameters are specified after the method name in a class, inside parentheses. You can add as many parameters as you want but just separate them with a comma. Data can be passed to functions as a parameter. Actually, parameters act as variables inside the method.
Let’s take a look at an example to understand this.
You can also use any primitive data type or built-in Java class as a data type for the parameters or you can also use your own classes as parameter types.
So, this is about the method parameters in Java.
Now, let’s understand how to allocate memory for the methods that are called.
Memory allocation for methods calls
1 Methods calls are implemented through the stack.
2 Whenever a method is called by a stack, a frame is created within the stack area.
3 After that, the arguments are passed to and the local variables and the value to be returned by this called method are stored in the stack frame.
4 When the execution of the called method is finished, the allocated stack frame would be deleted.
5 There is also a stack pointer register that tracks the top of the stack which can be adjusted accordingly.
So, this is about the method parameters in Java.
Now, let’s understand how to allocate memory for the methods that are called.
Memory allocation for methods calls
1 Methods calls are implemented through the stack.
2 Whenever a method is called by a stack, a frame is created within the stack area.
3 After that, the arguments are passed to and the local variables and the value to be returned by this called method are stored in the stack frame.
4 When the execution of the called method is finished, the allocated stack frame would be deleted.
5 There is also a stack pointer register that tracks the top of the stack which can be adjusted accordingly.
public class Madam
{
public static void main(String[] args) { int a = 5; System.out.println(cube(a)); } static int cube(int theNum) { return theNum * theNum * theNum; } }
{
public static void main(String[] args) { int a = 5; System.out.println(cube(a)); } static int cube(int theNum) { return theNum * theNum * theNum; } }
Anonymous Poll
51%
125
27%
Compilation Error because cube is already defined in the java.lang.Math class.
12%
Throws an ArithmeticException.
10%
Compilation Error or Runtime Error for some other reason.
You need to subscribe, in order to get notification for the next tutorials. and do comment if you have any suggession.