Methods in Java

In the previous section, we have discussed about the structure of class in java. The first component of class is variable. We have understood the type of variable and uses of the same in our previous section. Now in this section we will discuss on the second component of the method which is called “Methods in java”.

Objective:

  • What is Method
  • Method Signature
  • Components of Method

What is Method?

Methods in java is nothing but a function which is defined inside the class to manipulate the instance variables. As we know instance variable is declared inside the class but outside the method/function. If we want to perform some action or want to use the instance variable to achieve some target then we need to use method inside the class.

Method can access instance variable directly without any access specifier. The syntax to declare the method is as below.

Syntax:

modifiers return_type  method_name (parameter)
{
            Method_body
}

From the above syntax you can notice that method declaration has 6 components. Such as

  • Modifiers
  • Return Type (return_type)
  • Method Name (method_name)
  • Parameter(parameter)
  • Method body
  • Exception

Let’s discuss each component one by one.

Modifiers:

Modifiers generally specifies about the access of the method. That mead from where the method should be accessible. In other words, this is also called “Access Specifiers”.

In java, we have 4 access specifiers.

  • Public
  • Private
  • Protected
  • Default

If we set the modifiers / access specifier as public then you can access the method in all class in the application.

Unlike public, Private allows you to access the method only within the class.

Protected allows you the access the method within the class and within its subclasses but not outside of the classes.

Default access specifiers allows you to access the method with the same class and package where the class has defined. We will discuss about the package in upcoming tutorials. If you have not defined any specifiers in the method then this will take as by-default access specifiers/ modifiers for the method.

Note : If no Access Specifier / Modifier is specified then it will take the default access specifier and will be accessed within the class and package within which the class is defined.

Return Type:

return_type indicates the type of the data which is returned by method. As we know a method should return some value or no value. If a method is returning any value, then the type of the same returned data should be mentioned in the method declaration. If a method does not returning any value i.e. it is only manipulating the data then the return type should be “void”.

From the above we have cleared that a method should have a return type either it is a data type or a void.

Note: We have two types of return type. i.e. data type & void

Method Name:

From the name of the component you can notice that this indicates he name of the method. This is up to you what method name you specify. By convention, the method name should be a verb with lower case. It also consist of multiple words but the first letter of the second word should be the capital letter.

Apart from this the rule for defining the field should also applicable to method name.

e.g. run, compareTo, isEmpty etc.

Parameter:

Parameter is another important component of method in java. This is considered as input value for the method. If you want to give some input value to the method declaration, then the same need be mentioned within the () with the method name.

You also can provide the list of parameter you want with the data type inside the () with the method name. If you wish not to provide any parameter then you can leave the () as blank.

Leaving () as blank will indicate method not to take any input parameter.

e.g. public int run(int x, float, y)

Method body:

This is the heart of the Methods in java. This will define the actual work for which method is intended. You can write any java code inside the method which will be performed when this method is executed.

Exception:

Exception is the optional component of the method. If you need to handle some exception inside the method then you can mention that in method signature. We will discuss about this in out upcoming tutorial.

Now you are clear about the structure of the method and use of every component of method. Let’s discuss in briefly with example.

Example:-1- Simple method declaration

class Rectangle
{
	double width; 
	double height; 
	double area()
{
		return width*height;
	}
}

class AreaOfRectangle
{
	public static void main(String args[])
{
		Rectangle rect = new Rectange();
		double area; 
		rect.width = 10;
		rect.height = 20;
		area = rect.area();
		System.out.println("Area is : " + area);
	}
}

Output:

Area is : 200.0

Explanation:

From the above example you can notice that we have declared a method inside the Rectangle class named as area. The purpose of the area() method is to calculate the area of the rectangle. As we know area of rectangle should be a double. So we have defined the return type as double before the method name. Before return type we have not mentioned any access specifier. So, by-default the access specifier will be “default”. So you can access this method within the same class or inside the package in which Rectangle class is defined.

To calculate area we need width & height of rectangle. This 2 variable is declared as instance variable. So we do we can access this 2 variable inside the method directly. So we do not need any parameter to method to pass the value to the method. That’s why we have not mentioned any parameter with the method signature.

As we have defined the return type as double then we need to return some double value from the method. That’s why we have written below line inside the method body. This will return the value to the method.

 return width*height;

To instantiate the Methods in java we need a object. We will discuss this later.

Example:-1- Parameterized method declaration

class Rectangle{
	double width; 
	double height; 
	double area(){
		return width*height;
	}
	void setValue(double w, double h)
	{
		width = w;
		height = h;
	}
}

class AreaOfRectangle{
	public static void main(String args[]){
		Rectangle rect = new Rectangle();
		Rectangle rect2 = new Rectangle();
		double area_of_rect; 
		double area_of_rect2; 
		rect.setValue(10,20);
		rect2.setValue(3, 2);
		area_of_rect = rect.area();
		area_of_rect2 = rect2.area();
		System.out.println("Area of Rect is : " + area_of_rect + " & Arear of Rect2 is :  "+ area_of_rect2);
	}
}

Output:

Area of Rect is : 200.0 & Area of Rect2 is :  6.0

Explanation :

The above example is same as Example-1 – Simple method declaration. Only difference is the parameter in Method signature. If you want to give the value of width & height dynamically with the method object then you need to use parameterized method. This is called parameterized method because parameter is added in the method signature.

Leave a Reply

Your email address will not be published. Required fields are marked *