“Missing return statement” within if / for / while in Java

0 votes

I have this method which returns a String value, but my problem is whenever I am executing this code the compiler is returning the error missing return statement. What am I missing here?

public String myMethod()
{
    if(condition)
    {
       return xyz;
    }
}
Sep 26, 2018 in Java by v.liyyah
• 1,290 points
842 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Putting a return statement in any of the iterative statements may or may not return value. Problem with your code is that you need to provide an else condition for the cases where if condition isn't true. This is why you are getting the error.

But in case you write if/else block, each with a return statement in it then compiler then it will not force you. Thus, you need to write your code as follows:

if(condition)
{
 return;
}
else
{
 return;
}
answered Sep 26, 2018 by code.reaper12
• 3,450 points

Related Questions In Java

0 votes
1 answer

Performance difference of if/else vs switch statement in Java

The thing you are worried about is ...READ MORE

answered Jul 26, 2018 in Java by geek.erkami
• 2,320 points
239 views
+1 vote
2 answers

How to generate random integers within specific range in Java?

You can achieve that concisely in Java: Random ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
34 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,040 points
512 views
0 votes
1 answer

I am learning looping statements. Can you tell me how 'for-each' works in Java?

While programming we often write code that ...READ MORE

answered Apr 17, 2018 in Java by Rishabh
• 3,520 points
32 views
0 votes
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,520 points
39 views
0 votes
1 answer

What are optional parameters in Java

Using three dots: public void move(Object... x) { ...READ MORE

answered Apr 27, 2018 in Java by developer_1
• 3,280 points
46 views
0 votes
1 answer

How to split Strings by space in Java ?

You can use split() method. str = "Hello ...READ MORE

answered May 16, 2018 in Java by sharth
• 3,310 points
55 views
0 votes
2 answers

How to clear the console in Java?

import java.io.IOException; public class CLS { ...READ MORE

answered Aug 27, 2018 in Java by Sushmita
• 6,840 points
3,260 views
0 votes
1 answer

Need for finalize() in Java

finalize() is a method called by the ...READ MORE

answered May 8, 2018 in Java by code.reaper12
• 3,450 points
59 views
0 votes
1 answer

Overloaded method for null in Java

The method invoked here will be the ...READ MORE

answered May 23, 2018 in Java by code.reaper12
• 3,450 points
45 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.