Methods in Java

Hello readers, in today’s topic, we will discuss methods in Java. These are very important concepts that are necessary to remember before writing a Java program. Before these, we also discussed Decision making & loop control in Java, if you don’t know Hurry up Now! Previously we also describe briefly about various topics of Java such as Operator in Java, Data types of Java, Difference between C++ & Java, and many more which is really helpful for your better understanding. So without wasting time let’s dive into our topic:

Methods in Java

A method is a self-contained block of statements or a sub-program of one or more statements that performs a special task when it is called. We can directly pass data in a method called parameters as a passing value. In Java, methods are used to perform certain specific actions and they are also known as functions.

Why do we use methods?

We use a method to reuse the specific code. It means we have to define the code at once and use it several times. Sometimes our program grows in size and we want to separate the logic of the main method from other methods.

For Instance: – To calculate an average of a number pain 5 times then we have to use the method concept in Java to avoid repeating the logic (DRY-Don’t repeat yourself).

Syntax of Method:-

A method in Java is a function that must be declared/written within a class. The way of defining a method is it declares the name of the method followed by two parentheses (). Science java is an object-oriented programming language we need to write the method inside some class.

Syntax: –

datatype/return type method_name(){
	//body of the method;
	//statement;
	//code;
}

Example:-

public class main{
	static void sum(int a, int b){
		int c = a+b;
		return c;
	}
}
  • In the above program here sum() is the name of the method.
  • In sum(int a, int b) int a and int b are the parameters.
  • And static means that the method belongs to the main class and not an object of the Main class.
  • void means here in this method it does not have any return value.

Calling a Method

To call a method in Java, we have to write the method name followed by two parentheses () and a semicolon;

Or

In Java a method is called by creating an object of the class in which the method is present is followed by the method call.

GeektoCode obj = new GeektoCode(); //object creation
obj.sum(a, b); //Method call through the object

A method can also be called multiple times. The value from the method call (a & b) is copied to the a & b of the function sum(). Thus even if we modify the values a and b inside the method, the values in the main method will not change.

Return value

When we don’t want our method to return anything indicates that the method should not return a value. If we want the method to return a value, we can use a primitive data type (such as int, char, etc) instead of void and use the ‘return‘ keyword inside the method.

Static keyword

static keyword in java is used to associate a method of a given class with the class rather than the object static method in a class is shared by all the objects.

Process of method invocation in Java

consider the method sum:

int sum(int a, int b){
      return a+b;
}

The method is called like this:

GeektoCOde obj = new GeektoCode();
c = obj.sum(2,3);

The value of 2 & 3 are copied to a & b and then a+b = 2+3 = 5 is returned in c which is an integer.
Note: In the case of an array, the reference is passed, same is the case for an object passing to the methods.

So this is all about the Methods in Java. I hope this is helpful to you. In the next upcoming post we will discuss another new topic of java, so wait for this will meet in the next post. Thank you.

Leave a Reply

Your email address will not be published.

Sorry
Sorry
Sorry
Sorry
Sorry
Sorry
Sorry