Methods in java
It is a block of code or group of statements to perform certain
task/operation.
Code Reusability purpose
Can not create one method inside another method.
Can call any method inside another method.
Method must have meaningful name
Method declaration
public static void methodName()
{
System.out.println(“ xyz ”);
}
Types of methods
1) System Defined Method / Pre defined Method
Already defined in java library
Main Method Other Pre-defined Methods
public static void main(String[] args) 1) println();
{
2) print()
System.out.println(“ ABC ”);
3) length();
}
4) equal();
It is called by jvm automatically when we run program
Code execution start from main method
2) User defined Methods/Regular Methods
Can not execute automatically
To execute need to call in main method or any another method
Regular Method
public static void firstMethod()
{
System.out.println(“ ABC ”);
Two Types
1) Static Method
2) Non-static Method / instance method
1) Static Method 2) Non-static Method / instance method
public static void firstMethod()
public void firstMethod()
{
{
System.out.println(“ ABC ”);
System.out.println(“ ABC ”);
}
}
Static method called directly in any method Non-static method can not called directly with name
Static method does not require Object to call To call Non-Static method need an Object.
Memory allocation for static method at the time Memory allocation for non-static method at the time
of compilation. of run
Key Point
If programs stuck in infinite loop, at the end it will throw stackOverFlowError.
Allocated call Stack memory exceed.