How many types of method in java, and whats the use of method in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How many types of method in java, and whats the use of method in java

I have read different different pages oo methods but I am not understand this topic

6th Jul 2022, 2:33 PM
Mohammad Abdullah
Mohammad Abdullah - avatar
3 Answers
+ 3
Mohammad Abdullah Java programs have 1 "main" method. This "main" method is written in most programs The main method-- public static void main(String args[]) {} This is the entry point of the program, just like a building has gate to enter. If Java was a building then the "main" method would be it's gate. After that, Java allows you to create your "own" / "customised” methods. These could be created for various tasks and can be different for different purposes. Methods are meant to divide the tasks. Like, big companies/organisations have large programs. If they will write all program in the same place (main method) it will create confusion and work won't be divided properly. It is like doing all the homework/classwork of all the subjects in a single notebook.This is will create confusions like, for which subject is the cw/hw and you will not find your notes on time. Instead, divide the cw/hw in multiple notebooks and simplify the job. Easier to handle.
6th Jul 2022, 3:43 PM
Bhaveshsingh Pawar
Bhaveshsingh Pawar - avatar
+ 3
Mohammad Abdullah Based on access/scope in the program methods could be- Public: accessible in the whole class and outside the class Private: Accessible "only" in the class. Methods can "return" values. That means they when they are called in program they can give back a value. Say.. public int calculate_sum(int a, int b) { return a+b; } In this method, "int" here is the return type, that means when this method will be called it will give back an integer value, this value would be "a+b". The "int a" and "int b" are parameters which are given when calling the method. In your program say, you have a main method public static void main(String args[]) { int a = 2; int b = 2; int sum = calculate_sum(a,b); } Over here your method "calculate_sum" is called and the value that it "returns" is stored in the variable sum. This was about methods in simple sense.
6th Jul 2022, 3:43 PM
Bhaveshsingh Pawar
Bhaveshsingh Pawar - avatar
+ 2
A method in java is is a block of code that perform some action and may returns a result. It has syntax to follow. Depending on return type : void and non - void methods.. Depending on access modifiers : public, private, default, protected methods.. Depending on access specifiers: static and instance methods.. Ex : public static void display() { //code } here display() is a method with no arguments and public, static method, does not return anything... Any method in java is a member of a class so must be enclosed in a class.. . Hope it helps....
6th Jul 2022, 3:43 PM
Jayakrishna 🇮🇳