Differences Between Void and Non-Void Methods in Java

24 Jun 2025 | 4 min read

Methods can be classified into void methods and non-void methods based on whether they return a value or not.

Java Methods

A method in Java is a block of code that performs a specific task. Methods improve code reusability, modularity, and readability. Methods can be:

  • Predefined (like System.out.println())
  • User-defined (like addNumbers())

Void Methods

Void methods are methods that do not return any value. It is primarily used to operate, such as displaying output, modifying a variable, or updating a database. Void methods are ideal when an action must be performed, but no data is needed after the method completes.

Since it does not return a value, the method's return type is declared as void.

Syntax:

Advantages

  • Easy to write and understand.
  • Best for simple, one-time actions.
  • Useful for UI or console output.

Disadvantages

  • No feedback or result is returned.
  • Cannot participate in expressions.

When to Use Non-Void Methods?

  • Calculating the total bill amount.
  • Fetching data from a database.
  • Checking user credentials (for example, boolean validateUser())

Example of Void Method

Output:

Hello, this is a void method!

Explanation: The method displayMessage() performs a task (printing) but does not return anything. The void keyword in its declaration indicates it. In the main() method, the object obj is used to call the method, and the message is printed to the console.

Non-Void Methods

A non-void method returns a value of a specific data type, such as int, double, String, or even an object. The return type is explicitly declared in the method signature, and the method must use a return statement to return a value. Non-void methods are used when a method is supposed to perform a calculation or fetch a result that must be used elsewhere in the program.

Syntax:

Advantages

  • Return valuable data for further processing.
  • Improve code modularity and reusability.
  • Enable decision-making with return values.

Disadvantages

  • Must handle the return type appropriately.
  • Slightly more complex in terms of return logic.

When to Use Void Methods?

  • Logging errors to a file.
  • Updating UI components.
  • Displaying information to users.

Example of Non-Void Methods

Output:

Sum: 15

Explanation: The method add() takes two integers, calculates their sum, and uses a return statement to provide the result. The result can then be stored in a variable or used in expressions.

Differences Between Void and Non-Void Methods

Aspect

Void Method

Non-Void Method

Return Type

void (does not return any value)

Returns a specific data type (for example, int, double, String).

Return Statement

It does not require a return statement.

It requires a return statement with a value of the specified type.

Usage

It is used when only performing an action (for example, printing, modifying a variable).

It is used when a value needs to be computed and returned for further use.

Calling Method

It is called independently, and does not provide a return value

It is called and typically assigned to a variable or used in an expression.

Example

void displayMessage()

int add(int a, int b)

Call Behavior

obj.displayMessage();

int sum = obj.add(5, 10);

Void and Non-Void Methods MCQs

1. What does a void method return?

  1. 0
  2. Null
  3. Nothing
  4. "void"
 

Answer: c)

Explanation: A void method in Java returns nothing. That's literally what void means-it indicates the absence of a return value.


2. Which of the following is a valid void method declaration?

  1. int methodName()
  2. void methodName()
  3. methodName void()
  4. void:methodName()
 

Answer: b)

Explanation: A valid void method declaration in Java includes the access modifier, the void return type, a method name, parentheses (with or without parameters), and a method body.


3. Which keyword is used to return a value from a non-void method?

  1. exit
  2. break
  3. return
  4. stop
 

Answer: c)

Explanation: In a non-void method, return is used to send a value back to the caller.


4. Which method type allows you to assign its result to a variable?

  1. Void
  2. Non-void
  3. Static
  4. Final
 

Answer: b)

Explanation: In Java, if a method has a return type (like int, String, boolean, etc.), you can assign its result to a variable.


5. What happens if a non-void method does not return a value?

  1. Compiles with warning
  2. Compilation error
  3. Executes normally
  4. Returns null
 

Answer: b)

Explanation: If a non-void method in Java does not return a value, the compiler throws an error, because it is expecting a return statement that matches the declared return type.