The perfect place for easy learning...

Java Programming

×

Topics List


Java Methods





A method is a block of statements under a name that gets executes only when it is called. Every method is used to perform a specific task. The major advantage of methods is code re-usability (define the code once, and use it many times).

In a java programming language, a method defined as a behavior of an object. That means, every method in java must belong to a class.

Every method in java must be declared inside a class.

Every method declaration has the following characteristics.

  • returnType - Specifies the data type of a return value.
  • name - Specifies a unique name to identify it.
  • parameters - The data values it may accept or recieve.
  • { } - Defienes the block belongs to the method.

Creating a method

A method is created inside the class and it may be created with any access specifier. However, specifying access specifier is optional.

Following is the syntax for creating methods in java.

Syntax
class <ClassName>{
    <accessSpecifier> <returnType> <methodName>( parameters ){
        ...
        block of statements;
        ...
    }
}

🔔 The methodName must begin with an alphabet, and the Lower-case letter is preferred.

🔔 The methodName must follow all naming rules.

🔔 If you don't want to pass parameters, we ignore it.

🔔 If a method defined with return type other than void, it must contain the return statement, otherwise, it may be ignored.


Calling a method

In java, a method call precedes with the object name of the class to which it belongs and a dot operator. It may call directly if the method defined with the static modifier. Every method call must be made, as to the method name with parentheses (), and it must terminate with a semicolon.

Syntax
<objectName>.<methodName>( actualArguments );

🔔 The method call must pass the values to parameters if it has.

🔔 If the method has a return type, we must provide the receiver.


Let's look at the following example java code.

Example
import java.util.Scanner;
public class JavaMethodsExample {
	int sNo;
	String name;
	Scanner read = new Scanner(System.in);
	
	void readData() {
		System.out.print("Enter Serial Number: ");
		sNo = read.nextInt();
		System.out.print("Enter the Name: ");
		name = read.next();
	}
	
	static void showData(int sNo, String name) {
		System.out.println("Hello, " + name + "! your serial number is " + sNo);
	}
	

	public static void main(String[] args) {
		
		JavaMethodsExample obj = new JavaMethodsExample();
		
		obj.readData();   // method call using object
		
		showData(obj.sNo, obj.name);  // method call without using object

	}

}

Output:

methods and classes in java

🔔 The objectName must begin with an alphabet, and a Lower-case letter is preferred.

🔔 The objectName must follow all naming rules.


Variable arguments of a method

In java, a method can be defined with a variable number of arguments. That means creating a method that receives any number of arguments of the same data type.

Syntax
<returnType> <methodName>(dataType...parameterName);

Let's look at the following example java code.

Example
public class JavaMethodWithVariableArgs {
	
	void diaplay(int...list) {
		
		System.out.println("\nNumber of arguments: " + list.length);
		
		for(int i : list) {
			System.out.print(i + "\t");
		}
		
	}
	

	public static void main(String[] args) {
		
		JavaMethodWithVariableArgs obj = new JavaMethodWithVariableArgs();
		
		obj.diaplay(1, 2);
		obj.diaplay(10, 20, 30, 40, 50);

	}

}

Output:

methods and classes in java

🔔 When a method has both the normal parameter and variable-argument, then the variable argument must be specified at the end in the parameters list.


Constructor

A constructor is a special method of a class that has the same name as the class name. The constructor gets executes automatically on object creation. It does not require the explicit method call. A constructor may have parameters and access specifiers too. In java, if you do not provide any constructor the compiler automatically creates a default constructor.

Let's look at the following example java code.

Example
public class ConstructorExample {

	ConstructorExample() {
		System.out.println("Object created!");
	}
	public static void main(String[] args) {

		ConstructorExample obj1 = new ConstructorExample();
		ConstructorExample obj2 = new ConstructorExample();
	}

}

Output:

constructor in java

🔔 A constructor can not have return value.