---
title: "How to declare and invoke methods in Java?"  
description: "How to declare and invoke methods in Java?"  
author: "Ashutosh Patel"  
published: 2025-03-21  
updated: 2025-03-21  
canonical: https://www.mindstick.com/forum/161310/how-to-declare-and-invoke-methods-in-java  
category: "java"  
tags: ["java", "methods", "java method"]  
reading_time: 2 minutes  

---

# How to declare and invoke methods in Java?

How to declare and [invoke](https://www.mindstick.com/interview/983/how-will-you-invoke-any-external-process-in-java) methods in Java?

## Replies

### Reply by Amrith Chandran

## Method in Java

A method in Java is a block of code that performs a specific task. It improves the reusability and modularity of the code.

## Declaring a Method

The below syntax is for declaring a method in java,

```java
returnType methodName(parameters) {
   // Method body (statements)
   return value; // (if returnType is not void)
}
```

## Components of a method declaration:

- **Return type** - Specifies the data type of the value to be returned by the method. If no value is returned, use `void`.
- **Method name** - Follows standard naming conventions (CamelCase).
- **Parameters** (optional) - The values ​​passed to the method inside the brackets.
- **Method body** - The actual code inside {} that is executed when the method is called.
- **Return statement** (optional) - Used if the method returns a value.

## Example

```java
class Example {
   // Method with no parameters and no return value
   void sayHello() {
       System.out.println("Hello, World!");
   }
}
```

## Invoking (Calling) a Method

To execute a method, we need to call it using its name.

## Syntax

```java
objectName.methodName(arguments);
```

If the method is `static` type then call it using its **class name**

```java
ClassName.methodName(arguments);
```

## Example

```java
class ClassExample {
   // Method Declaration
   void greet() {
       System.out.println("Welcome to Java!");
   }
   public static void main(String[] args) {
       ClassExample objName = new ClassExample(); // Creating an object
       objName.greet(); // Calling the method
   }
}
```

## Ouput

```plaintext
Welcome to Java!
```

Also, Read: [Types of Methods in Java](https://www.mindstick.com/articles/338819/types-of-methods-in-java)


---

Original Source: https://www.mindstick.com/forum/161310/how-to-declare-and-invoke-methods-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
