Method and Default method in Java

A method is a block of code that perform some action which executes only on calling. Data can be pass in the form of parameters. Methods are also known as functions.

Example

class C {
public static void display() {

}
}

Method Name: display() is a method name and it can be any user defined name followed by opening and closing parenthesis().

void: It is a kind of return type. We will be learning more about return type later in this chapter.

static: This mean method belongs to C class and it can be called without object name.

  1. Static method can only access static methods and data variables.

2. display() method can be called directly with the class name.

C.display();

3. display() method can also be called using class instance.

C obj= new C();

obj. display() // Called display function using class instance obj.

4. static methods and variables can be access directly.

Method Calling:

Method Parameters and Arguments:

Data can be passed as method parameters or arguments. There can be n number of parameters define within the parenthesis as comma separated after method name.

Single Parameter and Argument:

public static void display (String name): highlighted name is the parameter in display() function. one and two passed as a parameter value for display() function.

Multiple Parameters and Arguments:

public static void display (String name, int id): highlighted name and is are parameters and arguments in display() function.

Method return type:

There are different method return type depending on the return value. Below are the different return types:

Void -> void means empty. This return type doesn’t return anything.

Int, String, float, Char -> This will return primitive data type.

Example:

In the below code snippet sum(int a ) method is having a return type as an integer value.

Default Method

Java 8 introduces a new technique where it allows us to write implementation of default method in interfaces.

Single interface can have any number of default method implementation.

It provides us the backward compatibility to add some functionality for existing interfaces without changing existing logic of classes implementation the same interface.

For example, if we have an interface and same is implementing by multiple classes. After adding a default method or methods will not force us to update the existing classes implementing the same interface.

Syntax:

Static default methods:

Similar to default method interface can also have default static method implementation.

Example:

Below is the example for defining default and static methods in an interface.

Syntax:

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan