What is Method?
?
- A method is a block of code that performs a specific task in programming.
- It can take input (called parameters) and return an output.
- Methods help reuse code and make programs easier to manage.
Naming Convention a Method
- Method names should use camelCase.
- Method names should be short and meaningful.
Method Calling & Method Declaration Syntax
?
class ClassName{
public static void main(String[] args){
ClassName objectName = new ClassName();
// Method Calling
objectName.methodName();
}
// Method Signature
returnType dataType methodName(){
// set of instruction
return ;
}
}
- Objectname + Mehtodname (Activity Name)
- objectName.methodName() - objectName-மிருக்கு பிறகு dot(.) வைத்து methodName() குடுக்க வேண்டும்.
- () - inputs கொடுப்பதற்கு பயன்படுத்துவோம்.
- Method definition உள்ளே இன்னொரு method call செய்யலாம். Ex: Main Method
Example:
class Calculator{
public static void main(String[] args){
Calculator casio = new Calculator();
// Method Calling
int result = casio.add(4,5);
System.out.println(result);
}
// Method Signature
int add(int num1, int num2){
// Method Defination
int c = num1 + num2;
return c;
}
}
How many types of methods in java?
?
- Pre-defined Methods: These are built-in methods in Java that are available to use.
- User-defined Methods: We can create our own method based on our requirements.
- Static Method
- Instance Method
- Abstract Method
- Factory Method
Main Method
Method means activity.
Important Activity - Main Method
- Program execution starts from main method
- Main method inside the class or part of class.